Commit c8b692feb879a93f14befbbf373bbbbb6dc05fcc

  • avatar
  • arvind
  • Tue Mar 04 10:40:39 IST 2014
Fix:
    More code refactor following inputs from ecthiender. Removing
    PostedCommentView. A comment being editable or not is now
    handled by a single view.
app.js
(26 / 33)
  
44 // C.CommentView = CommentView; //Expose the API to window.
55 // C.Comment = Comment;
66 /* Create a empty model and view */
7 C.comment = new Comment();
78 C.comments = new Comments();
8 C.cv = new PostedCommentView({collection: C.comments});
9 new CommentView({model: new Comment()});
9 C.cv = new CommentView({collection: C.comments});
10 C.comments.add(C.comment);
1011 };
1112 var Comment = Backbone.Model.extend({
1213 /* A model representing a comment. */
7373
7474 var CommentView = Backbone.View.extend({
7575 el: $("#comments"),
76 template: _.template($("#comment-template").html()),
76 template: _.template($("#commented-template").html()),
77 edit_template: _.template($("#comment-template").html()),
7778 events:{
78 "click .save": "save"
79 "click .save": "save",
80 "click .comment button": "reply"
7981 },
8082 initialize: function() {
81 this.render();
83 this.listenTo(this.collection, "add", this.render);
8284 },
8385
84 render: function(el) {
85 $(this.el).append(this.template(this.model.toJSON()));
86 render: function(model) {
87 console.log(model);
88 if(model.get('how')['comment'].length) {
89 $(this.el).append(this.template(model.toJSON()));
90 }
91 else {
92 $(this.el).append(this.edit_template(model.toJSON()));
93 }
94
95
8696 },
8797
8898 save: function(e) {
104104 this.model.set({created: new Date().toUTCString().substr(0, 25)});
105105 new LoginView({model:this.model});
106106
107 }
108 });
109
110 var PostedCommentView = Backbone.View.extend({
111 el: "#commented",
112 events: {
113 "click button": "reply"
114107 },
115 template: _.template($("#commented-template").html()),
116 initialize: function() {
117 this.listenTo(this.collection, "add", this.render);
118 this.render();
119 },
120 render: function(el) {
121 _.each(this.collection.models, function(comment) {
122 console.log(comment);
123 $(this.el).append(this.template(comment.toJSON()));
124 }, this);
125
126 },
127108 reply: function(e) {
128 var rep = new Comment({'how':{'replyTo':$(e.currentTarget).attr('for')}});
129 $(e.currentTarget).parent().after("<div class='comment-reply'></div>");
130 var el = $("#commented .comment-reply");
131 new CommentView({model: rep, el:el });
109 e.preventDefault();
110 var rep = new Comment();
111 rep.set({'how':{'replyTo':$(e.currentTarget).attr('for'),
112 'comment':''}});
113 C.comments.add(rep);
132114 }
133
134115 });
116
135117 var LoginView = Backbone.View.extend({
136118 el: ".modal",
137119 events:{
141141 C.comments.add(model);
142142 $("textarea.form-control").val(""); //Reset the view to have no content.
143143 }
144 });
145 }});
144 });
145 }});
146146 }
147147 });
148148