PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/models/models.js

http://github.com/jslatts/nodechat
JavaScript | 121 lines | 86 code | 23 blank | 12 comment | 18 complexity | 1e5c5fd6bf6e17c97aa162b5b6170024 MD5 | raw file
  1. (function () {
  2. var server = false, models;
  3. if (typeof exports !== 'undefined') {
  4. _ = require('underscore')._;
  5. Backbone = require('backbone');
  6. models = exports;
  7. server = true;
  8. } else {
  9. models = this.models = {};
  10. }
  11. //
  12. //models
  13. //
  14. models.ChatEntry = Backbone.Model.extend({});
  15. models.UserModel = Backbone.Model.extend({
  16. idAttribute: "name"
  17. });
  18. models.NodeChatModel = Backbone.Model.extend({
  19. initialize: function() {
  20. this.chats = new models.ChatCollection();
  21. this.chats.comparator = datetimeComparator;
  22. this.users = new models.UserCollection();
  23. this.users.comparator = nameComparator;
  24. }
  25. });
  26. //
  27. //Collections
  28. //
  29. models.ChatCollection = Backbone.Collection.extend({
  30. model: models.ChatEntry
  31. });
  32. models.UserCollection = Backbone.Collection.extend({
  33. model: models.UserModel
  34. });
  35. var nameComparator = function (topic) {
  36. return topic.get('name');
  37. }
  38. var datetimeComparator = function(chat) {
  39. var datetime = chat.get('time');
  40. if(datetime) return datetime;
  41. else return 0;
  42. }
  43. //
  44. //Model exporting/importing
  45. //
  46. Backbone.Model.prototype.xport = function (opt) {
  47. var result = {},
  48. settings = _({recurse: true}).extend(opt || {});
  49. function process(targetObj, source) {
  50. targetObj.id = source.id || null;
  51. targetObj.cid = source.cid || null;
  52. targetObj.attrs = source.toJSON();
  53. _.each(source, function (value, key) {
  54. // since models store a reference to their collection
  55. // we need to make sure we don't create a circular refrence
  56. if (settings.recurse) {
  57. if (key !== 'collection' && source[key] instanceof Backbone.Collection) {
  58. targetObj.collections = targetObj.collections || {};
  59. targetObj.collections[key] = {};
  60. targetObj.collections[key].models = [];
  61. targetObj.collections[key].id = source[key].id || null;
  62. _.each(source[key].models, function (value, index) {
  63. process(targetObj.collections[key].models[index] = {}, value);
  64. });
  65. } else if (source[key] instanceof Backbone.Model) {
  66. targetObj.models = targetObj.models || {};
  67. process(targetObj.models[key] = {}, value);
  68. }
  69. }
  70. });
  71. }
  72. process(result, this);
  73. return JSON.stringify(result);
  74. };
  75. Backbone.Model.prototype.mport = function (data, silent) {
  76. function process(targetObj, data) {
  77. targetObj.id = data.id || null;
  78. targetObj.set(data.attrs, {silent: silent});
  79. // loop through each collection
  80. if (data.collections) {
  81. _.each(data.collections, function (collection, name) {
  82. targetObj[name].id = collection.id;
  83. _.each(collection.models, function (modelData, index) {
  84. var newObj = targetObj[name]._add({}, {silent: silent});
  85. process(newObj, modelData);
  86. });
  87. });
  88. }
  89. if (data.models) {
  90. _.each(data.models, function (modelData, name) {
  91. process(targetObj[name], modelData);
  92. });
  93. }
  94. }
  95. process(this, JSON.parse(data));
  96. return this;
  97. };
  98. })()