Commit c8b692feb879a93f14befbbf373bbbbb6dc05fcc
- Diff rendering mode:
- inline
- side by side
app.js
(26 / 33)
  | |||
4 | 4 | // C.CommentView = CommentView; //Expose the API to window. | |
5 | 5 | // C.Comment = Comment; | |
6 | 6 | /* Create a empty model and view */ | |
7 | C.comment = new Comment(); | ||
7 | 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 | 12 | var Comment = Backbone.Model.extend({ | |
12 | 13 | /* A model representing a comment. */ | |
… | … | ||
73 | 73 | ||
74 | 74 | var CommentView = Backbone.View.extend({ | |
75 | 75 | el: $("#comments"), | |
76 | template: _.template($("#comment-template").html()), | ||
76 | template: _.template($("#commented-template").html()), | ||
77 | edit_template: _.template($("#comment-template").html()), | ||
77 | 78 | events:{ | |
78 | "click .save": "save" | ||
79 | "click .save": "save", | ||
80 | "click .comment button": "reply" | ||
79 | 81 | }, | |
80 | 82 | initialize: function() { | |
81 | this.render(); | ||
83 | this.listenTo(this.collection, "add", this.render); | ||
82 | 84 | }, | |
83 | 85 | ||
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 | }, | |
87 | 97 | ||
88 | 98 | save: function(e) { | |
… | … | ||
104 | 104 | this.model.set({created: new Date().toUTCString().substr(0, 25)}); | |
105 | 105 | new LoginView({model:this.model}); | |
106 | 106 | ||
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 | 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 | 117 | var LoginView = Backbone.View.extend({ | |
136 | 118 | el: ".modal", | |
137 | 119 | events:{ | |
… | … | ||
141 | 141 | C.comments.add(model); | |
142 | 142 | $("textarea.form-control").val(""); //Reset the view to have no content. | |
143 | 143 | } | |
144 | }); | ||
145 | }}); | ||
144 | }); | ||
145 | }}); | ||
146 | 146 | } | |
147 | 147 | }); | |
148 | 148 |