Commit 0e02398e10fe025735cd6cd38e339ed8ce0e21c3

  • avatar
  • arvind
  • Thu Mar 06 22:23:54 IST 2014
Cleaning up more code. Threading is implemented visually and the collection has
also obtained a comparator object.
Visually threading is oldest first.
app.js
(23 / 32)
  
2626 var Comment = Backbone.Model.extend({
2727 /* A model representing a comment. */
2828 url: function() {
29 return C.url + C.sweet;
30 },
29 return C.url + C.sweet;
30 },
3131 defaults: function() {
3232 return {
3333 'who':'',
8383 }
8484 }
8585 });
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 }
8693 }
87
8894 });
8995
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 // });
9996 var CommentView = Backbone.View.extend({
97 el: $("#comments-container"),
10098 tagName: 'div',
10199 template: _.template($("#commented-template").html()),
102100 edit_template: _.template($("#comment-template").html()),
102102 "click .save": "save",
103103 "click .comment button": "reply"
104104 },
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();
108112 },
109113 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()));
120116 }
121117 else {
122 $(this.el).append(this.edit_template(this.model.toJSON()));
118 $(this.el).append(this.template(this.model.toJSON()));
119
123120 }
124 return this;
125121
126 },
122 },
127123
128124 save: function(e) {
129125 /* Create a sweet and send it to the sweet store.
127127 e.preventDefault();
128128
129129 this.model.get('how').set({'comment':this.$("textarea.form-control").val()});
130 console.log(this.model.get('how'));
131130 this.model.set({created: new Date().toUTCString().substr(0, 25)});
132131 new LoginView({model:this.model});
133132
172172 });
173173
174174 var AppView = Backbone.View.extend({
175 el: $("#comments"),
176175 initialize: function() {
177176 this.listenTo(C.comments, "add", this.showOne);
178177
179179 showOne: function(model) {
180180 if(model.get('how').isValid === undefined) {
181181 model.set({'how': new How(model.get('how'))});
182 console.log(model);
183182 }
184183 var view = new CommentView({model:model});
185 $(this.el).append(view.render().el);
184
186185 }
187186
188187 });