Created
August 29, 2013 17:48
-
-
Save joelongstreet/6381186 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define([ | |
'chaplin', | |
'models/base/model' | |
], function(Chaplin, Model) { | |
'use strict'; | |
var Collection = Chaplin.Collection.extend({ | |
model : Model, | |
parms : {}, | |
listen : function(){ | |
var collection = this; | |
socket.request(this.url, this.params, function(results){ | |
collection.add(results); | |
}); | |
// I think this handler might have to be way more sophisticated | |
// TODO: | |
// Drop socket when collection is dropped, i thought chaplin said it would unbind | |
// everything if the registration was done correctly | |
// Do param matching either here, or on the server. I'm thinking it would be easier | |
// to do it on the client, but better to do it on the server | |
socket.on('message', function(message){ | |
if(message.verb == 'create') collection.add(message.data) | |
else if(message.verb == 'destroy') collection.remove(message.id) | |
else if(message.verb == 'update'){ | |
var model = collection.get(message.id) | |
if(model){ | |
model.set(message.data); | |
} | |
} | |
console.log(message); | |
}); | |
} | |
}); | |
return Collection; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment