Guest User

Untitled

a guest
Jul 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. <script src="socket.io/socket.io.js"></script>
  2. <script src="/coffee/object-sync-client.js"></script>
  3. <script>
  4. sync = new ObjectSync();
  5. sync.connect();
  6. sync.on('update', function(obj) {
  7. switch(obj.type) {
  8. case 'player':
  9. drawPlayer(obj);
  10. break;
  11. case 'score':
  12. updateScore(obj);
  13. break;
  14. // ... etc
  15. }
  16. });
  17. sync.on('destroy', function(id){/*...*/});
  18. sync.on('create', function(obj){/*...*/});
  19.  
  20. # Create a new object (saving something without an id)
  21. sync.save({
  22. is_this_new: 'yes'
  23. }, function(err, obj) {
  24. // returns either an error or a server-provided object
  25. // if there was no error, a 'create' event will fire here
  26. // and on every other connected client
  27. });
  28. sync.save({
  29. id: 5,
  30. is_this_new: false,
  31. is_this_updated: true
  32. }, function(err, obj){
  33. // updates an object
  34. });
  35. sync.fetch([1,2,3,4], function(err, results) {
  36. // fetches objects 1,2,3 and 4
  37. });
  38. sync.destroy(1, function(err) {
  39. // if there was no error, object 1 is now destroyed
  40. // a 'destroy' event will fire shortly
  41. })
  42. sync.allObjects(); // returns a map of all objects on the client
  43. </script>
Add Comment
Please, Sign In to add comment