PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/labs/architecture-examples/backbone.xmpp/js/lib/backbone.xmpp.node.js

https://github.com/1manStartup/todomvc
JavaScript | 95 lines | 61 code | 18 blank | 16 comment | 9 complexity | df3f3a3596d97343367d513124ab2ff9 MD5 | raw file
  1. // Backbone XMPP PubSub Storage v0.3
  2. // (c) 2012 Yiorgis Gozadinos.
  3. // Backbone.xmpp is distributed under the MIT license.
  4. // http://github.com/ggozad/Backbone.xmpp
  5. // A simple model/collection using **Backbone.xmpp.storage** and supporting XMPP
  6. // notifications. Can be used to base your models upon.
  7. (function ($, _, Backbone, Strophe, PubSubStorage) {
  8. // PubSub Item
  9. var PubSubItem = Backbone.Model.extend({
  10. sync: Backbone.xmppSync
  11. });
  12. // PubSub Items collection
  13. var PubSubNode = Backbone.Collection.extend({
  14. model: PubSubItem,
  15. node: null,
  16. sync: Backbone.xmppSync,
  17. // **initialize** expects the id of the node to be passed in `options`
  18. // as well as the Strophe connection.
  19. // If you do not know it ahead you should add the `node` attribute and
  20. // subscribe to the XMPP events manually.
  21. initialize: function (models, options) {
  22. options = options || {};
  23. if (options.id && options.connection) {
  24. this.setNode(options.id, options.connection, options.payloadFormat);
  25. }
  26. },
  27. setNode: function(id, connection, format) {
  28. if (this.node) {
  29. connection.PubSub.off('xmpp:pubsub:item-published:' + this.node.id, this.onItemPublished, this);
  30. connection.PubSub.off('xmpp:pubsub:item-deleted:' + this.node.id, this.onItemDeleted, this);
  31. }
  32. this.node = new PubSubStorage(id, connection, format);
  33. connection.PubSub.on('xmpp:pubsub:item-published:' + id, this.onItemPublished, this);
  34. connection.PubSub.on('xmpp:pubsub:item-deleted:' + id, this.onItemDeleted, this);
  35. },
  36. // **onItemPublished** is a subscriber to the `xmpp:pubsub:item-published` event.
  37. // When a model has been pushed to the server from a different client, it will be
  38. // received and added automatically to the collection, triggering an `add` event.
  39. // If the model already existed it will be updated triggering a `change` event.
  40. onItemPublished: function (item) {
  41. var payload = item.entry,
  42. self = this,
  43. d = $.Deferred(),
  44. existing,
  45. json;
  46. d.promise().done(function () {
  47. existing = self.get(item.id),
  48. json = self.node.parseItem(payload);
  49. if (existing) {
  50. self.remove(existing, {silent: true});
  51. self.add(existing, {at: 0, silent: true});
  52. existing.set(json);
  53. } else {
  54. json.id = item.id;
  55. self.add(json, {at: 0});
  56. }
  57. });
  58. if (payload) {
  59. d.resolve();
  60. } else {
  61. this.node.connection.PubSub.items(this.node.id, {item_ids: [item.id]})
  62. .done(function (res) {
  63. payload = $('entry', res);
  64. d.resolve();
  65. });
  66. }
  67. },
  68. onItemDeleted: function (item) {
  69. item = this.get(item.id);
  70. if (item) {
  71. this.remove(item);
  72. }
  73. }
  74. });
  75. this.PubSubItem = PubSubItem;
  76. this.PubSubNode = PubSubNode;
  77. })(this.jQuery, this._, this.Backbone, this.Strophe, this.PubSubStorage);