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 59 ---------------------------------++++++++++++++++++++++++++
  • Diff rendering mode:
  • inline
  • side by side

app.js

4 // C.CommentView = CommentView; //Expose the API to window.4 // C.CommentView = CommentView; //Expose the API to window.
5 // C.Comment = Comment;5 // C.Comment = Comment;
6 /* Create a empty model and view */6 /* Create a empty model and view */
7 C.comment = new Comment();
7 C.comments = new Comments();8 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);
10 };11 };
11 var Comment = Backbone.Model.extend({12 var Comment = Backbone.Model.extend({
12 /* A model representing a comment. */13 /* A model representing a comment. */
7373
74 var CommentView = Backbone.View.extend({74 var CommentView = Backbone.View.extend({
75 el: $("#comments"),75 el: $("#comments"),
76 template: _.template($("#comment-template").html()),
76 template: _.template($("#commented-template").html()),
77 edit_template: _.template($("#comment-template").html()),
77 events:{78 events:{
78 "click .save": "save"
79 "click .save": "save",
80 "click .comment button": "reply"
79 },81 },
80 initialize: function() {82 initialize: function() {
81 this.render();
83 this.listenTo(this.collection, "add", this.render);
82 },84 },
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
86 },96 },
8797
88 save: function(e) {98 save: function(e) {
104 this.model.set({created: new Date().toUTCString().substr(0, 25)});104 this.model.set({created: new Date().toUTCString().substr(0, 25)});
105 new LoginView({model:this.model});105 new LoginView({model:this.model});
106106
107 }
108 });
109
110 var PostedCommentView = Backbone.View.extend({
111 el: "#commented",
112 events: {
113 "click button": "reply"
114 },107 },
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 },
127 reply: function(e) {108 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);
132 }114 }
133
134 });115 });
116
135 var LoginView = Backbone.View.extend({117 var LoginView = Backbone.View.extend({
136 el: ".modal",118 el: ".modal",
137 events:{119 events:{
141 C.comments.add(model);141 C.comments.add(model);
142 $("textarea.form-control").val(""); //Reset the view to have no content.142 $("textarea.form-control").val(""); //Reset the view to have no content.
143 }143 }
144 });
145 }});
144 });
145 }});
146 }146 }
147 });147 });
148148