server/src/main/webapp/js/main_test_models.js
changeset 45 37c9a17c3284
equal deleted inserted replaced
43:90f6937c417e 45:37c9a17c3284
       
     1 (function() {
       
     2 	
       
     3 	var root = this;
       
     4 		
       
     5 	var Backbone = root.Backbone;
       
     6 	
       
     7 	var Rkns = root.Rkns = {};
       
     8 
       
     9 	root.ModelView = Backbone.View.extend({
       
    10 		
       
    11 		events : {
       
    12 			"click .show_form" : "show_form",
       
    13 			"click .remove_button" : "remove_click",
       
    14 		},
       
    15 		
       
    16 		initialize: function(options) {
       
    17 			_.bindAll(this);
       
    18 			this.template = _.template(options.template);
       
    19 			this.model.bind("change", this.render);
       
    20 			this.form_view = options.form_view;
       
    21 			this.form_el = options.form_el;
       
    22 			if(typeof options.getJSON === "function") {
       
    23 				this.getJSON = options.getJSON;
       
    24 			}
       
    25 		},
       
    26 		render: function() {
       
    27 			$(this.el).html(this.template(this.getJSON()));
       
    28 			return this;
       
    29 		},
       
    30 		show_form: function(evt) {
       
    31 			if(typeof this.form_view !== "undefined") {
       
    32 				this.form_view.render();
       
    33 				$(this.form_el).empty();
       
    34 				$(this.form_el).append(this.form_view.el);
       
    35 			}
       
    36 		},
       
    37 		getJSON : function() {
       
    38 			return this.model.toJSON();			
       
    39 		},
       
    40 		remove_click : function(evt) {
       
    41 			var obj_id = $(evt.currentTarget).attr("value");
       
    42 			if(typeof this.model.collection !== "undefined") {
       
    43 				this.model.collection.remove(this.model);
       
    44 			}
       
    45 		},
       
    46 
       
    47 	});
       
    48 	
       
    49 	root.ColView = Backbone.View.extend({
       
    50 		initialize: function(options) {
       
    51 			
       
    52 			_.bindAll(this);
       
    53 			this.element = options.element;
       
    54 			this.element_form = options.element_form;
       
    55 			this.element_form_el = options.element_form_el;
       
    56 			this.rendered = false;
       
    57 			
       
    58 			var that = this;
       
    59 			this.element_views = [];
       
    60 			
       
    61 			this.collection.each(function(elt) { 
       
    62 				this.add(elt); 
       
    63 			});
       
    64 			
       
    65 			this.collection.bind('add', this.add);
       
    66 			this.collection.bind('remove', this.remove);
       
    67 		},
       
    68 		
       
    69 		render : function() {
       
    70 			this.rendered = true;
       
    71 			var that = this;
       
    72 			this.$el.empty();
       
    73 			_(this.element_views).each(function(dvs){
       
    74 				that.$el.append(dvs.disp.render().el);				
       
    75 			});
       
    76 			
       
    77 			return this;
       
    78 		},
       
    79 		
       
    80 		add : function(elt) {
       
    81 			
       
    82 			var fv_args = _.extend({}, this.element_form, {model: elt});			
       
    83 			var fv = new FormView(fv_args);
       
    84 			
       
    85 			var dv_args = _.extend({}, this.element, {model: elt, tagName: 'li', form_view: fv, form_el: this.element_form_el});			
       
    86 			var dv = new ModelView(dv_args);
       
    87 			
       
    88 			this.element_views.push({disp:dv, form:fv});
       
    89 			
       
    90 			if(this.rendered) {
       
    91 				this.$el.append(dv.render().el);
       
    92 			}
       
    93 		},
       
    94 			
       
    95 		
       
    96 		remove : function(elt) {
       
    97 			var viewsToRemove = _(this.element_views).select(function(cvs) { return cvs.disp.model === elt; })[0];
       
    98 			this.element_views = _(this.element_views).without(viewsToRemove);
       
    99 			
       
   100 			if(this.rendered) {
       
   101 				viewsToRemove.disp.$el.remove();
       
   102 				viewsToRemove.form.$el.remove();
       
   103 			}
       
   104 		},
       
   105 		
       
   106 		remove_click : function(evt) {
       
   107 			var obj_id = $(evt.currentTarget).attr("value");
       
   108 			this.collection.remove(this.collection.get(obj_id));
       
   109 		}
       
   110 		
       
   111 	});
       
   112 	
       
   113 	root.FormView = Backbone.View.extend({
       
   114 		events : {
       
   115 			"change .form_field": "changed"
       
   116 		},
       
   117 		initialize: function(options) {
       
   118 			_.bindAll(this);
       
   119 			this.template = _.template(options.template);
       
   120 			this.model.bind("change", this.render);
       
   121 			if(typeof options.getJSON === "function") {
       
   122 				this.getJSON = options.getJSON;
       
   123 			}
       
   124 		},
       
   125 		render: function() {
       
   126 			this.$el.html(this.template(this.getJSON()));
       
   127 			return this;
       
   128 		},
       
   129 		changed: function(evt) {
       
   130 			var value = $(evt.currentTarget).val();
       
   131 			var field_name = $(evt.currentTarget).attr("name");
       
   132 			if(this.model.get(field_name) != value) {
       
   133 				this.model.set(field_name, value);
       
   134 			}
       
   135 		},
       
   136 		getJSON : function() {
       
   137 			return this.model.toJSON(); 
       
   138 		}
       
   139 	});
       
   140 	
       
   141 	
       
   142 	root.SelectCurrentUserView = Backbone.View.extend({
       
   143 		initialize: function(options) {
       
   144 			_.bindAll(this);
       
   145 			this.rendered = false;
       
   146 			
       
   147 			this.child_view_constructor = options.child_view_constructor;
       
   148 			this.child_tag_name = options.child_tag_name;
       
   149 			
       
   150 			this.children_views = [];
       
   151 			this.collection.each(this.add);
       
   152 			
       
   153 			this.collection.bind('add', this.add);
       
   154 			this.collection.bind('remove', this.remove);
       
   155 		},
       
   156 		add: function(elt) {
       
   157 			var child_view = new this.child_view_constructor({
       
   158 				model: elt,
       
   159 				tagName : this.child_tag_name
       
   160 			});
       
   161 			this.children_views.push(child_view);
       
   162 			
       
   163 			if (this.rendered) {
       
   164 				this.$el.append(child_view.render().el);
       
   165 			}
       
   166 		},
       
   167 		remove: function(elt) {
       
   168 			var view_to_remove = _(this.children_views).select(function(cv) { return cv.model === elt; })[0];
       
   169 			this.children_views = _(this.children_views).without(view_to_remove);
       
   170 			if (this.rendered) {
       
   171 				view_to_remove.$el.remove();
       
   172 			}
       
   173 		},
       
   174 		render: function() {
       
   175 			this.rendered = true;
       
   176 			var that = this;
       
   177 			
       
   178 			this.$el.empty().html("<option value=\"\">Unknown</option>");
       
   179 			
       
   180 			_(this.children_views).each(function(cv){
       
   181 				that.$el.append(cv.render().el);
       
   182 			});
       
   183 			
       
   184 			return this;
       
   185 		}
       
   186 		
       
   187 	});
       
   188 	
       
   189 	root.SelectUserView = Backbone.View.extend({
       
   190 		initialize: function(options) {
       
   191 			_.bindAll(this);
       
   192 			this.model.bind("change", this.render);
       
   193 		},		
       
   194 		render: function() {
       
   195 			this.$el.val(this.model.id).text(this.model.get("title"));
       
   196 			return this;
       
   197 		}
       
   198 	});
       
   199 	
       
   200 	root.CurrentUserView = Backbone.View.extend({
       
   201 		initialize : function() {
       
   202 			this._select_view = new SelectCurrentUserView({
       
   203 				el : this.el,
       
   204 				collection : this.model.get("users"),
       
   205 				child_view_constructor: SelectUserView,
       
   206 				child_tag_name: "option",
       
   207 			});
       
   208 		},
       
   209 		render: function() {
       
   210 			this.$el.empty();
       
   211 			this._select_view.render();
       
   212 			return this;
       
   213 		}
       
   214 		
       
   215 	});
       
   216 	
       
   217 	
       
   218 	root.BasicListView = Backbone.View.extend({
       
   219 		
       
   220 		initialize: function(options) {
       
   221 			_.bindAll(this);
       
   222 			
       
   223 			this.element_view_constructor = options.element_view_constructor;
       
   224 			if(typeof options.template !== "undefined") {
       
   225 				this.template = _.template(options.template);
       
   226 			}
       
   227 			
       
   228 			this.rendered = false;
       
   229 			this.element_views = [];
       
   230 			this.collection.each(this.add);
       
   231 			
       
   232 			this.collection.bind('add', this.add);
       
   233 			this.collection.bind('remove', this.remove);
       
   234 			this.collection.bind('reset', this.reset);
       
   235 			this.collection.bind('destroy', this.destroy);
       
   236 		},
       
   237 	
       
   238 		render: function() {
       
   239 			this.rendered = true;
       
   240 			var that = this;
       
   241 			this.$el.empty();
       
   242 			if(typeof this.template !== "undefined") {
       
   243 				var data = {};
       
   244 				if(typeof this.model !== "undefined") {
       
   245 					data = this.$el.model.toJSON();
       
   246 				}
       
   247 				this.$el.html(this.template(data));
       
   248 			}
       
   249 			_(this.element_views).each(function(dvs){
       
   250 				that.$el.append(dvs.render().el);
       
   251 			});
       
   252 
       
   253 			return this;
       
   254 		},
       
   255 		
       
   256 		add: function(elt) {
       
   257 			var element_view = new this.element_view_constructor({model: elt});
       
   258 			this.element_views.push(element_view);
       
   259 			if(this.rendered) {
       
   260 				this.$el.append(element_view.render().el);
       
   261 			}
       
   262 		},
       
   263 		
       
   264 		remove: function(elt) {
       
   265 			var element_view = _(this.element_views).select(function(cv) {return cv.model === elt;})[0];
       
   266 			this.element_views = _(this.element_views).without(element_view);
       
   267 			if(this.rendered) {
       
   268 				element_view.$el.remove();
       
   269 			}
       
   270 			
       
   271 		},
       
   272 
       
   273 		reset: function() {
       
   274 			_(this.element_views).each(function(cv) {
       
   275 				cv.$el.remove();
       
   276 			});
       
   277 			this.element_views = [];
       
   278 			this.collection.each(this.add);	
       
   279 		}
       
   280 	
       
   281 	});
       
   282 	
       
   283 	
       
   284 	root.BasicModelView = Backbone.View.extend({
       
   285 				
       
   286 		initialize: function(options) {
       
   287 			_.bindAll(this);
       
   288 			if(typeof options.template !== "undefined") {
       
   289 				this.template = _.template(options.template);
       
   290 			}
       
   291 			if(typeof options.getJSON === "function") {
       
   292 				this.getJSON = options.getJSON;
       
   293 			}
       
   294 			this.model.bind("change", this.render);
       
   295 		},
       
   296 		render: function() {
       
   297 			$(this.el).html(this.template(this.getJSON()));
       
   298 			return this;
       
   299 		},
       
   300 		getJSON : function() {
       
   301 			return this.model.toJSON();			
       
   302 		}
       
   303 	});
       
   304 	
       
   305 }).call(this);