1 (function() { |
1 (function() { |
2 "use strict"; |
2 "use strict"; |
3 var root = this; |
3 var root = this; |
4 |
4 |
5 var Backbone = root.Backbone; |
5 var Backbone = root.Backbone; |
6 |
6 |
7 var Models = root.Rkns.Models = {}; |
7 var Models = root.Rkns.Models = {}; |
8 |
8 |
9 |
9 |
10 Models.getUID = function(obj) { |
10 Models.getUID = function(obj) { |
11 var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { |
11 var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { |
12 var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); |
12 var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8); |
13 return v.toString(16); |
13 return v.toString(16); |
14 }); |
14 }); |
15 return obj.type + "-" + guid; |
15 return obj.type + "-" + guid; |
16 }; |
16 }; |
17 |
17 |
18 |
18 |
19 var RenkanModel = Backbone.RelationalModel.extend({ |
19 var RenkanModel = Backbone.RelationalModel.extend({ |
20 idAttribute : "_id", |
20 idAttribute : "_id", |
21 constructor: function(options) { |
21 constructor: function(options) { |
22 |
22 |
23 if (typeof options !== "undefined") { |
23 if (typeof options !== "undefined") { |
24 options._id = options._id || options.id || Models.getUID(this); |
24 options._id = options._id || options.id || Models.getUID(this); |
25 options.title = options.title || ""; |
25 options.title = options.title || ""; |
26 options.description = options.description || ""; |
26 options.description = options.description || ""; |
27 options.uri = options.uri || ""; |
27 options.uri = options.uri || ""; |
28 |
28 |
29 if(typeof this.prepare === "function") { |
29 if(typeof this.prepare === "function") { |
30 options = this.prepare(options); |
30 options = this.prepare(options); |
31 } |
31 } |
32 } |
32 } |
33 Backbone.RelationalModel.prototype.constructor.call(this, options); |
33 Backbone.RelationalModel.prototype.constructor.call(this, options); |
207 var _user = User.findOrCreate(_props); |
207 var _user = User.findOrCreate(_props); |
208 this.get("users").push(_user, _options); |
208 this.get("users").push(_user, _options); |
209 return _user; |
209 return _user; |
210 }, |
210 }, |
211 addNode: function(_props, _options) { |
211 addNode: function(_props, _options) { |
212 _props.project = this; |
212 _props.project = this; |
213 var _node = Node.findOrCreate(_props); |
213 var _node = Node.findOrCreate(_props); |
214 this.get("nodes").push(_node, _options); |
214 this.get("nodes").push(_node, _options); |
215 return _node; |
215 return _node; |
216 }, |
216 }, |
217 addEdge: function(_props, _options) { |
217 addEdge: function(_props, _options) { |
256 initialize: function() { |
256 initialize: function() { |
257 var _this = this; |
257 var _this = this; |
258 this.on("remove:nodes", function(_node) { |
258 this.on("remove:nodes", function(_node) { |
259 _this.get("edges").remove( |
259 _this.get("edges").remove( |
260 _this.get("edges").filter(function(_edge) { |
260 _this.get("edges").filter(function(_edge) { |
261 return _edge.get("from") == _node || _edge.get("to") == _node; |
261 return _edge.get("from") === _node || _edge.get("to") === _node; |
262 }) |
262 }) |
263 ); |
263 ); |
264 }); |
264 }); |
265 } |
265 } |
266 }); |
266 }); |
267 |
267 |
268 var RosterUser = Models.RosterUser = Backbone.Model.extend({ |
268 var RosterUser = Models.RosterUser = Backbone.Model.extend({ |
269 type: "roster_user", |
269 type: "roster_user", |
270 idAttribute : "_id", |
270 idAttribute : "_id", |
271 |
271 |
272 constructor: function(options) { |
272 constructor: function(options) { |
273 |
273 |
274 if (typeof options !== "undefined") { |
274 if (typeof options !== "undefined") { |
275 options._id = options._id || options.id || Models.getUID(this); |
275 options._id = options._id || options.id || Models.getUID(this); |
276 options.title = options.title || "(untitled " + this.type + ")"; |
276 options.title = options.title || "(untitled " + this.type + ")"; |
277 options.description = options.description || ""; |
277 options.description = options.description || ""; |
278 options.uri = options.uri || ""; |
278 options.uri = options.uri || ""; |
279 options.project = options.project || null; |
279 options.project = options.project || null; |
280 options.site_id = options.site_id || 0; |
280 options.site_id = options.site_id || 0; |
281 |
281 |
282 if(typeof this.prepare === "function") { |
282 if(typeof this.prepare === "function") { |
283 options = this.prepare(options); |
283 options = this.prepare(options); |
284 } |
284 } |
285 } |
285 } |
286 Backbone.Model.prototype.constructor.call(this, options); |
286 Backbone.Model.prototype.constructor.call(this, options); |
287 }, |
287 }, |
288 |
288 |
289 validate: function() { |
289 validate: function() { |
290 if(!this.type) { |
290 if(!this.type) { |
291 return "object has no type"; |
291 return "object has no type"; |
292 } |
292 } |
293 }, |
293 }, |
294 |
294 |
295 prepare: function(options) { |
295 prepare: function(options) { |
296 options.color = options.color || "#666666"; |
296 options.color = options.color || "#666666"; |
297 return options; |
297 return options; |
298 }, |
298 }, |
299 |
299 |
300 toJSON: function() { |
300 toJSON: function() { |
301 return { |
301 return { |
302 _id: this.get("_id"), |
302 _id: this.get("_id"), |
303 title: this.get("title"), |
303 title: this.get("title"), |
304 uri: this.get("uri"), |
304 uri: this.get("uri"), |