forked from jslatts/nodechat-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.js
More file actions
115 lines (93 loc) · 3.3 KB
/
Copy pathmodels.js
File metadata and controls
115 lines (93 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
(function () {
var server = false, models;
if (typeof exports !== 'undefined') {
_ = require('underscore')._;
Backbone = require('backbone');
models = exports;
server = true;
} else {
models = this.models = {};
}
//
//models
//
models.User = Backbone.Model.extend({});
models.ChatEntry = Backbone.Model.extend({});
models.ClientCountModel = Backbone.Model.extend({
defaults: {
"clients": 0
},
updateClients: function(clients){
this.set({clients: clients});
}
});
models.NodeChatModel = Backbone.Model.extend({
defaults: {
"clientId": 0
},
initialize: function() {
this.chats = new models.ChatCollection();
}
});
//
//Collections
//
models.ChatCollection = Backbone.Collection.extend({
model: models.ChatEntry
});
//
//Model exporting/importing
//
Backbone.Model.prototype.xport = function (opt) {
var result = {},
settings = _({recurse: true}).extend(opt || {});
function process(targetObj, source) {
targetObj.id = source.id || null;
targetObj.cid = source.cid || null;
targetObj.attrs = source.toJSON();
_.each(source, function (value, key) {
// since models store a reference to their collection
// we need to make sure we don't create a circular refrence
if (settings.recurse) {
if (key !== 'collection' && source[key] instanceof Backbone.Collection) {
targetObj.collections = targetObj.collections || {};
targetObj.collections[key] = {};
targetObj.collections[key].models = [];
targetObj.collections[key].id = source[key].id || null;
_.each(source[key].models, function (value, index) {
process(targetObj.collections[key].models[index] = {}, value);
});
} else if (source[key] instanceof Backbone.Model) {
targetObj.models = targetObj.models || {};
process(targetObj.models[key] = {}, value);
}
}
});
}
process(result, this);
return JSON.stringify(result);
};
Backbone.Model.prototype.mport = function (data, silent) {
function process(targetObj, data) {
targetObj.id = data.id || null;
targetObj.set(data.attrs, {silent: silent});
// loop through each collection
if (data.collections) {
_.each(data.collections, function (collection, name) {
targetObj[name].id = collection.id;
_.each(collection.models, function (modelData, index) {
var newObj = targetObj[name]._add({}, {silent: silent});
process(newObj, modelData);
});
});
}
if (data.models) {
_.each(data.models, function (modelData, name) {
process(targetObj[name], modelData);
});
}
}
process(this, JSON.parse(data));
return this;
};
})()