PageRenderTime 17ms CodeModel.GetById 1ms app.highlight 12ms RepoModel.GetById 1ms 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
  7        models = exports;
  8        server = true;
  9    } else {
 10        models = this.models = {};
 11    }
 12
 13    //
 14    //models
 15    //
 16    
 17    models.ChatEntry = Backbone.Model.extend({});
 18
 19    models.UserModel = Backbone.Model.extend({
 20        idAttribute: "name"
 21    });
 22
 23    models.NodeChatModel = Backbone.Model.extend({
 24        initialize: function() {
 25            this.chats = new models.ChatCollection(); 
 26            this.chats.comparator = datetimeComparator;
 27
 28            this.users = new models.UserCollection();
 29            this.users.comparator = nameComparator;
 30        }
 31    });
 32
 33    //
 34    //Collections
 35    //
 36    models.ChatCollection = Backbone.Collection.extend({
 37        model: models.ChatEntry
 38    });
 39
 40    models.UserCollection = Backbone.Collection.extend({
 41        model: models.UserModel
 42    });
 43
 44    var nameComparator = function (topic) {
 45        return topic.get('name');
 46    }
 47
 48    var datetimeComparator = function(chat) {
 49        var datetime = chat.get('time');
 50
 51        if(datetime) return datetime;
 52        else return 0;
 53    }
 54
 55
 56    //
 57    //Model exporting/importing
 58    //
 59    
 60    Backbone.Model.prototype.xport = function (opt) {
 61        var result = {},
 62        settings = _({recurse: true}).extend(opt || {});
 63
 64        function process(targetObj, source) {
 65            targetObj.id = source.id || null;
 66            targetObj.cid = source.cid || null;
 67            targetObj.attrs = source.toJSON();
 68            _.each(source, function (value, key) {
 69            // since models store a reference to their collection
 70            // we need to make sure we don't create a circular refrence
 71                if (settings.recurse) {
 72                  if (key !== 'collection' && source[key] instanceof Backbone.Collection) {
 73                    targetObj.collections = targetObj.collections || {};
 74                    targetObj.collections[key] = {};
 75                    targetObj.collections[key].models = [];
 76                    targetObj.collections[key].id = source[key].id || null;
 77                    _.each(source[key].models, function (value, index) {
 78                      process(targetObj.collections[key].models[index] = {}, value);
 79                    });
 80                  } else if (source[key] instanceof Backbone.Model) {
 81                    targetObj.models = targetObj.models || {};
 82                    process(targetObj.models[key] = {}, value);
 83                  }
 84               }
 85            });
 86        }
 87
 88        process(result, this);
 89
 90        return JSON.stringify(result);
 91    };
 92
 93
 94    Backbone.Model.prototype.mport = function (data, silent) {
 95        function process(targetObj, data) {
 96            targetObj.id = data.id || null;
 97            targetObj.set(data.attrs, {silent: silent});
 98            // loop through each collection
 99            if (data.collections) {
100              _.each(data.collections, function (collection, name) {
101                targetObj[name].id = collection.id;
102                _.each(collection.models, function (modelData, index) {
103                  var newObj = targetObj[name]._add({}, {silent: silent});
104                  process(newObj, modelData);
105                });
106              });
107            }
108
109            if (data.models) {
110                _.each(data.models, function (modelData, name) {
111                    process(targetObj[name], modelData);
112                });
113            }
114        }
115
116        process(this, JSON.parse(data));
117
118        return this;
119    };
120
121})()