server/src/main/webapp/js/models.js
author ymh <ymh.work@gmail.com>
Mon, 20 Aug 2012 16:07:15 +0200
changeset 22 948283342245
child 45 37c9a17c3284
permissions -rw-r--r--
Session moderator + test in model
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") {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
	            options._id = options.id || Models.getUID(this);
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",
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
			relatedModel: User
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
		}],
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
		prepare: function(options) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
			project = options.project;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
			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
    73
			options.description = options.description || "";			
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
			return options;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
	});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
	
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
	// EDGE
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
	var Edge = Models.Edge = root.Rkns.Models.Edge = RenkanModel.extend({
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
		type: "edge",
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
		relations: [
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
		  {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
			type: Backbone.HasOne,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
			key: "created_by",
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
			relatedModel: User
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
		  {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
		    type: Backbone.HasOne,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
			key: "from",
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
			relatedModel: Node
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
		  },
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
		  {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
		    type: Backbone.HasOne,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
			key: "to",
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
			relatedModel: Node
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
		  },
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
		],
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
		prepare: function(options) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
			project = options.project;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
			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
   102
			this.addReference(options, "from", options.from, project.get("nodes"));
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
			this.addReference(options, "to", options.to, project.get("nodes"));
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
			return options;
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
	});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
		
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
	// PROJECT
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
	var Project = Models.Project = root.Rkns.Models.Project = RenkanModel.extend({
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
		type: "project",
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
		relations: [
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
		  {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
		    type: Backbone.HasMany,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
		    key: "users",
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
		    relatedModel: User,
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
		    reverseRelation: {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
	            key: 'project',
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
	            includeInJSON: '_id'
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
	        },
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
		  },
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: "nodes",
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
		    relatedModel: Node,
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: "edges",
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
		    relatedModel: Edge,
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
		addNode: function(_props) {
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
			_props.project = this;
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
			this.get("nodes").push(_props);
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
		}
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
	});
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
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
}).call(this);
948283342245 Session moderator + test in model
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148