server/src/main/webapp/js/models.js
author ymh <ymh.work@gmail.com>
Tue, 11 Dec 2012 00:01:41 +0100
changeset 46 7e132e2a48ca
parent 45 37c9a17c3284
permissions -rw-r--r--
add most of the model and repositories
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
(function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
	var root = this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
	var Backbone = root.Backbone;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
	var Models = root.Rkns.Models = {};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
	Models.getUID = function(obj) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
		var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
		    var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
		    return v.toString(16);
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
		});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
		return obj.type + "-" + guid; 
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
	};
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
	var RenkanModel = Backbone.RelationalModel.extend({
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
		idAttribute : "_id",
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
		constructor: function(options) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
			if (typeof options !== "undefined") {
45
37c9a17c3284 - add Spring
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    24
	            options._id = options._id || options.id || Models.getUID(this);
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
	            options.title = options.title || "(untitled " + this.type + ")";
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
	            options.description = options.description || "";
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
	            options.uri = options.uri || "";
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
	            
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
	            if(typeof this.prepare === "function") {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
	            	options = this.prepare(options);
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
	            }
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
			Backbone.RelationalModel.prototype.constructor.call(this, options);
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
		validate: function() {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
			if(!this.type) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
				return "object has no type";
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
			}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
		},
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
		addReference : function(_options, _propName, _list, _id, _default) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
		    var _element = _list.get(_id);
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
		    if (typeof _element === "undefined" && typeof _default !== "undefined") {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
		        _options[_propName ] = _default;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
		    }
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
		    else {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
		    	_options[_propName ] = _element;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
		    }
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
	});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
	// USER
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
	var User = Models.User = root.Rkns.Models.User = RenkanModel.extend({
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
		type: "user",
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
		prepare: function(options) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
			options.color = options.color || "#666666";
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
			return options;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
		}	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
	});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
	// NODE
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
	var Node = Models.Node = root.Rkns.Models.Node = RenkanModel.extend({
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
		type: "node",
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
		relations: [{
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
			type: Backbone.HasOne,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
			key: "created_by",
45
37c9a17c3284 - add Spring
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    68
			relatedModel: User,
37c9a17c3284 - add Spring
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    69
			reverseRelation : {
37c9a17c3284 - add Spring
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    70
				includeInJSON: '_id'
37c9a17c3284 - add Spring
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    71
			}
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
		}],
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
		prepare: function(options) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
			project = options.project;
45
37c9a17c3284 - add Spring
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    75
			delete options["project"];
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
			this.addReference(options, "created_by", project.get("users"), options.created_by, project.current_user);
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
			options.description = options.description || "";			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
			return options;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
	});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
	// EDGE
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
	var Edge = Models.Edge = root.Rkns.Models.Edge = RenkanModel.extend({
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
		type: "edge",
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
		relations: [
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
		  {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
			type: Backbone.HasOne,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
			key: "created_by",
45
37c9a17c3284 - add Spring
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    90
			relatedModel: User,
37c9a17c3284 - add Spring
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    91
			reverseRelation : {
37c9a17c3284 - add Spring
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    92
				includeInJSON: '_id'
37c9a17c3284 - add Spring
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
    93
			}
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
		  },
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
		  {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
		    type: Backbone.HasOne,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
			key: "from",
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
			relatedModel: Node
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
		  },
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
		  {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
		    type: Backbone.HasOne,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
			key: "to",
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
			relatedModel: Node
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
		  },
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
		],
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
		prepare: function(options) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
			project = options.project;
45
37c9a17c3284 - add Spring
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   108
			delete options.project;
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
			this.addReference(options, "created_by", options.created_by, project.get("users"), project.current_user);
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
			this.addReference(options, "from", options.from, project.get("nodes"));
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
			this.addReference(options, "to", options.to, project.get("nodes"));
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
			return options;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
	});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
	// PROJECT
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
	var Project = Models.Project = root.Rkns.Models.Project = RenkanModel.extend({
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
		type: "project",
45
37c9a17c3284 - add Spring
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   119
		current_user : null,
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
		relations: [
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
		  {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
		    type: Backbone.HasMany,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
		    key: "users",
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
		    relatedModel: User,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
		    reverseRelation: {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
	            key: 'project',
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
	            includeInJSON: '_id'
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
	        },
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
		  },
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
		  {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
		    type: Backbone.HasMany,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
		    key: "nodes",
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
		    relatedModel: Node,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
		    reverseRelation: {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
	            key: 'project',
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
	            includeInJSON: '_id'
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
	        },
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
		  },
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
		  {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
		    type: Backbone.HasMany,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
		    key: "edges",
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
		    relatedModel: Edge,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
		    reverseRelation: {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
	            key: 'project',
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
	            includeInJSON: '_id'
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
	        },
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
		  }
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
		],
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
		addNode: function(_props) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
			_props.project = this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
			this.get("nodes").push(_props);
45
37c9a17c3284 - add Spring
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   152
		},
37c9a17c3284 - add Spring
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   153
		addUser: function(_props) {
37c9a17c3284 - add Spring
ymh <ymh.work@gmail.com>
parents: 22
diff changeset
   154
			this.get("users").push(_props);
22
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
	});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
}).call(this);
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160