Created
February 6, 2011 09:22
-
-
Save jonastemplestein/813250 to your computer and use it in GitHub Desktop.
node-object-sync client example
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
<script src="socket.io/socket.io.js"></script> | |
<script src="/coffee/object-sync-client.js"></script> | |
<script> | |
sync = new ObjectSync(); | |
sync.connect(); | |
sync.on('update', function(obj) { | |
switch(obj.type) { | |
case 'player': | |
drawPlayer(obj); | |
break; | |
case 'score': | |
updateScore(obj); | |
break; | |
// ... etc | |
} | |
}); | |
sync.on('destroy', function(id){/*...*/}); | |
sync.on('create', function(obj){/*...*/}); | |
# Create a new object (saving something without an id) | |
sync.save({ | |
is_this_new: 'yes' | |
}, function(err, obj) { | |
// returns either an error or a server-provided object | |
// if there was no error, a 'create' event will fire here | |
// and on every other connected client | |
}); | |
sync.save({ | |
id: 5, | |
is_this_new: false, | |
is_this_updated: true | |
}, function(err, obj){ | |
// updates an object | |
}); | |
sync.fetch([1,2,3,4], function(err, results) { | |
// fetches objects 1,2,3 and 4 | |
}); | |
sync.destroy(1, function(err) { | |
// if there was no error, object 1 is now destroyed | |
// a 'destroy' event will fire shortly | |
}) | |
sync.allObjects(); // returns a map of all objects on the client | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment