Commit 0e02398e10fe025735cd6cd38e339ed8ce0e21c3
Cleaning up more code. Threading is implemented visually and the collection has
also obtained a comparator object.
Visually threading is oldest first.
- app.js 55 --------------------------------+++++++++++++++++++++++
| | | | 26 | var Comment = Backbone.Model.extend({ | 26 | var Comment = Backbone.Model.extend({ |
---|
27 | /* A model representing a comment. */ | 27 | /* A model representing a comment. */ |
---|
28 | url: function() { | 28 | url: function() { |
---|
29 | return C.url + C.sweet; | | return C.url + C.sweet; |
---|
30 | }, | | }, |
---|
| | 29 | return C.url + C.sweet; | | | 30 | }, |
---|
31 | defaults: function() { | 31 | defaults: function() { |
---|
32 | return { | 32 | return { |
---|
33 | 'who':'', | 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({ | | // var Sweets = Backbone.Collection.extend({ |
---|
91 | // model: Comment, | | // model: Comment, |
---|
92 | // url: C.url + C.get + '?what=' + C.what, | | // url: C.url + C.get + '?what=' + C.what, |
---|
93 | // initialize: function() { | | // initialize: function() { |
---|
94 | // this.sync('read', this, { | | // this.sync('read', this, { |
---|
95 | // url: this.url | | // url: this.url |
---|
96 | // }); | | // }); |
---|
97 | // } | | // } |
---|
98 | // }); | | // }); |
---|
99 | var CommentView = Backbone.View.extend({ | 96 | var CommentView = Backbone.View.extend({ |
---|
| | 97 | el: $("#comments-container"), |
---|
100 | tagName: 'div', | 98 | tagName: 'div', |
---|
101 | template: _.template($("#commented-template").html()), | 99 | template: _.template($("#commented-template").html()), |
---|
102 | edit_template: _.template($("#comment-template").html()), | 100 | edit_template: _.template($("#comment-template").html()), |
---|
… | | … | |
---|
102 | "click .save": "save", | 102 | "click .save": "save", |
---|
103 | "click .comment button": "reply" | 103 | "click .comment button": "reply" |
---|
104 | }, | 104 | }, |
---|
105 | intialize: function() { | | intialize: function() { |
---|
106 | this.listenTo(this.model, "save", this.render); | | 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 | render: function() { | 113 | render: function() { |
---|
110 | if(this.model.get('how').get('comment').length) { | | if(this.model.get('how').get('comment').length) { |
---|
111 | if(this.model.get('how').get('replyTo').length) { | | if(this.model.get('how').get('replyTo').length) { |
---|
112 | var item = '#' + this.model.get('how').get('replyTo'); | | var item = '#' + this.model.get('how').get('replyTo'); |
---|
113 | var el = $(item).parent(); | | var el = $(item).parent(); |
---|
114 | $(el).append(this.template(this.model.toJSON())); | | $(el).append(this.template(this.model.toJSON())); |
---|
115 | } | | } |
---|
116 | else{ | | else{ |
---|
117 | console.log(this.model.toJSON()); | | console.log(this.model.toJSON()); |
---|
118 | $(this.el).append(this.template(this.model.toJSON())); | | $(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 | else { | 117 | else { |
---|
122 | $(this.el).append(this.edit_template(this.model.toJSON())); | | $(this.el).append(this.edit_template(this.model.toJSON())); |
---|
| | 118 | $(this.el).append(this.template(this.model.toJSON())); | | | 119 | |
---|
123 | } | 120 | } |
---|
124 | return this; | | return this; |
---|
125 | | 121 | |
---|
126 | }, | | }, |
---|
| | 122 | }, | 127 | | 123 | |
---|
128 | save: function(e) { | 124 | save: function(e) { |
---|
129 | /* Create a sweet and send it to the sweet store. | 125 | /* Create a sweet and send it to the sweet store. |
---|
… | | … | |
---|
127 | e.preventDefault(); | 127 | e.preventDefault(); |
---|
128 | | 128 | |
---|
129 | this.model.get('how').set({'comment':this.$("textarea.form-control").val()}); | 129 | this.model.get('how').set({'comment':this.$("textarea.form-control").val()}); |
---|
130 | console.log(this.model.get('how')); | | console.log(this.model.get('how')); |
---|
131 | this.model.set({created: new Date().toUTCString().substr(0, 25)}); | 130 | this.model.set({created: new Date().toUTCString().substr(0, 25)}); |
---|
132 | new LoginView({model:this.model}); | 131 | new LoginView({model:this.model}); |
---|
133 | | 132 | |
---|
… | | … | |
---|
172 | }); | 172 | }); |
---|
173 | | 173 | |
---|
174 | var AppView = Backbone.View.extend({ | 174 | var AppView = Backbone.View.extend({ |
---|
175 | el: $("#comments"), | | el: $("#comments"), |
---|
176 | initialize: function() { | 175 | initialize: function() { |
---|
177 | this.listenTo(C.comments, "add", this.showOne); | 176 | this.listenTo(C.comments, "add", this.showOne); |
---|
178 | | 177 | |
---|
… | | … | |
---|
179 | showOne: function(model) { | 179 | showOne: function(model) { |
---|
180 | if(model.get('how').isValid === undefined) { | 180 | if(model.get('how').isValid === undefined) { |
---|
181 | model.set({'how': new How(model.get('how'))}); | 181 | model.set({'how': new How(model.get('how'))}); |
---|
182 | console.log(model); | | console.log(model); |
---|
183 | } | 182 | } |
---|
184 | var view = new CommentView({model:model}); | 183 | var view = new CommentView({model:model}); |
---|
185 | $(this.el).append(view.render().el); | | $(this.el).append(view.render().el); |
---|
| | 184 | | 186 | } | 185 | } |
---|
187 | | 186 | |
---|
188 | }); | 187 | }); |
---|