Commit 0e02398e10fe025735cd6cd38e339ed8ce0e21c3
- Diff rendering mode:
- inline
- side by side
app.js
(23 / 32)
  | |||
26 | 26 | var Comment = Backbone.Model.extend({ | |
27 | 27 | /* A model representing a comment. */ | |
28 | 28 | url: function() { | |
29 | return C.url + C.sweet; | ||
30 | }, | ||
29 | return C.url + C.sweet; | ||
30 | }, | ||
31 | 31 | defaults: function() { | |
32 | 32 | return { | |
33 | 33 | 'who':'', | |
… | … | ||
83 | 83 | } | |
84 | 84 | } | |
85 | 85 | }); | |
86 | }, | ||
87 | comparator: function(model1, model2) { | ||
88 | if(model1.get('created') < model2.get('created') && | ||
89 | model1.get('how')['replyTo'] == model2.get('how')['replyTo']) { | ||
90 | console.log(model1, model2); | ||
91 | return 1; | ||
92 | } | ||
86 | 93 | } | |
87 | |||
88 | 94 | }); | |
89 | 95 | ||
90 | // var Sweets = Backbone.Collection.extend({ | ||
91 | // model: Comment, | ||
92 | // url: C.url + C.get + '?what=' + C.what, | ||
93 | // initialize: function() { | ||
94 | // this.sync('read', this, { | ||
95 | // url: this.url | ||
96 | // }); | ||
97 | // } | ||
98 | // }); | ||
99 | 96 | var CommentView = Backbone.View.extend({ | |
97 | el: $("#comments-container"), | ||
100 | 98 | tagName: 'div', | |
101 | 99 | template: _.template($("#commented-template").html()), | |
102 | 100 | edit_template: _.template($("#comment-template").html()), | |
… | … | ||
102 | 102 | "click .save": "save", | |
103 | 103 | "click .comment button": "reply" | |
104 | 104 | }, | |
105 | intialize: function() { | ||
106 | this.listenTo(this.model, "save", this.render); | ||
107 | |||
105 | initialize: function() { | ||
106 | if(this.model.get('how').get('replyTo').length) { | ||
107 | var item = '#' + this.model.get('how').get('replyTo'); | ||
108 | this.setElement($(item)); | ||
109 | } | ||
110 | // this.listenTo(this.model.collection, "add", this.render); | ||
111 | this.render(); | ||
108 | 112 | }, | |
109 | 113 | render: function() { | |
110 | if(this.model.get('how').get('comment').length) { | ||
111 | if(this.model.get('how').get('replyTo').length) { | ||
112 | var item = '#' + this.model.get('how').get('replyTo'); | ||
113 | var el = $(item).parent(); | ||
114 | $(el).append(this.template(this.model.toJSON())); | ||
115 | } | ||
116 | else{ | ||
117 | console.log(this.model.toJSON()); | ||
118 | $(this.el).append(this.template(this.model.toJSON())); | ||
119 | } | ||
114 | if(this.model.isNew()) { | ||
115 | $(this.el).append(this.edit_template(this.model.toJSON())); | ||
120 | 116 | } | |
121 | 117 | else { | |
122 | $(this.el).append(this.edit_template(this.model.toJSON())); | ||
118 | $(this.el).append(this.template(this.model.toJSON())); | ||
119 | |||
123 | 120 | } | |
124 | return this; | ||
125 | 121 | ||
126 | }, | ||
122 | }, | ||
127 | 123 | ||
128 | 124 | save: function(e) { | |
129 | 125 | /* Create a sweet and send it to the sweet store. | |
… | … | ||
127 | 127 | e.preventDefault(); | |
128 | 128 | ||
129 | 129 | this.model.get('how').set({'comment':this.$("textarea.form-control").val()}); | |
130 | console.log(this.model.get('how')); | ||
131 | 130 | this.model.set({created: new Date().toUTCString().substr(0, 25)}); | |
132 | 131 | new LoginView({model:this.model}); | |
133 | 132 | ||
… | … | ||
172 | 172 | }); | |
173 | 173 | ||
174 | 174 | var AppView = Backbone.View.extend({ | |
175 | el: $("#comments"), | ||
176 | 175 | initialize: function() { | |
177 | 176 | this.listenTo(C.comments, "add", this.showOne); | |
178 | 177 | ||
… | … | ||
179 | 179 | showOne: function(model) { | |
180 | 180 | if(model.get('how').isValid === undefined) { | |
181 | 181 | model.set({'how': new How(model.get('how'))}); | |
182 | console.log(model); | ||
183 | 182 | } | |
184 | 183 | var view = new CommentView({model:model}); | |
185 | $(this.el).append(view.render().el); | ||
184 | |||
186 | 185 | } | |
187 | 186 | ||
188 | 187 | }); |