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