-
Notifications
You must be signed in to change notification settings - Fork 254
/
Copy pathnew_view.js
45 lines (34 loc) · 1016 Bytes
/
new_view.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<%= view_namespace %> || (<%= view_namespace %> = {});
<%= view_namespace %>.NewView = Backbone.View.extend({
template: JST["<%= jst 'new' %>"],
events: {
"submit #new-<%= singular_name %>": "save"
},
initialize: function() {
var self = this;
this.model = new this.collection.model();
this.model.bind("change:errors", function() {
self.render();
});
},
save: function(e) {
e.preventDefault();
e.stopPropagation();
this.model.unset("errors");
var self = this;
this.collection.create(this.model.toJSON(), {
success: function(<%= singular_name %>) {
self.model = <%= singular_name %>;
window.location.hash = "/" + self.model.id;
},
error: function(<%= singular_name %>, jqXHR) {
self.model.set({errors: $.parseJSON(jqXHR.responseText)});
}
});
},
render: function() {
this.$el.html(this.template(this.model.toJSON() ));
this.$("form").backboneLink(this.model);
return this;
}
});