Commit 04018d7bc0f7f83eb18463a5204ffe9128987354

  • avatar
  • arvind
  • Wed Mar 05 19:04:29 IST 2014
Nested models is a better way of representing of a comment model with
how attributes.
app.js
(70 / 30)
  
11(function(C) {
22
33 C.init = function() {
4 // C.CommentView = CommentView; //Expose the API to window.
5 // C.Comment = Comment;
64 /* Create a empty model and view */
7 C.comment = new Comment();
8 C.comments = new Comments();
9 C.cv = new CommentView({collection: C.comments});
10 C.comments.add(C.comment);
5 C.comment = new Comment({'how': new How()});
6 C.comments = new Sweets();
7 /* Create the app, the app sets up event handler for the Comments collection */
8 var App = new AppView;
9
10 C.comments.add(C.comment); //Add the empty model to the collection.
11
1112 };
13 var How = Backbone.Model.extend({
14 defaults: {
15 'comment':'',
16 'replyTo':''
17 },
18 initialize: function() {
19 }
20 });
1221 var Comment = Backbone.Model.extend({
1322 /* A model representing a comment. */
23 url: function() {
24 return C.url + C.sweet;
25 },
1426 defaults: function() {
1527 return {
1628 'who':'',
17 'what': C.what,
29 'what':C.what,
1830 'where': window.location.href,
19 'how':{
20 'comment':'',
21 'replyTo':''
22 }
31 'how': ''
2332 };
2433 },
25 url: "http://127.0.0.1:5001/sweets",
34
2635 initialize: function() {
27 this.set({"what": C.what});
36 },
37 toJSON: function() {
38 how = this.get('how').toJSON();
39 return this;
2840 }
2941 });
3042
31 var Comments = Backbone.Collection.extend({
43 var Sweets = Backbone.Collection.extend({
3244 model: Comment,
3345 url: C.url + C.sweet,
3446 initialize: function() {
8383
8484 });
8585
86 // var Sweets = Backbone.Collection.extend({
87 // model: Comment,
88 // url: C.url + C.get + '?what=' + C.what,
89 // initialize: function() {
90 // this.sync('read', this, {
91 // url: this.url
92 // });
93 // }
94 // });
8695 var CommentView = Backbone.View.extend({
87 el: $("#comments"),
96 tagName: 'div',
8897 template: _.template($("#commented-template").html()),
8998 edit_template: _.template($("#comment-template").html()),
9099 events:{
91100 "click .save": "save",
92101 "click .comment button": "reply"
93102 },
94 initialize: function() {
95 this.listenTo(this.collection, "add", this.render);
96 },
103 intialize: function() {
104 this.listenTo(this.model, "save", this.render);
97105
98 render: function(model) {
99 console.log(model);
100 if(model.get('how')['comment'].length) {
101 $(this.el).append(this.template(model.toJSON()));
106 },
107 render: function() {
108 if(this.model.get('how').get('comment').length) {
109 if(this.model.get('how').get('replyTo').length) {
110 var item = '#' + this.model.get('how')['replyTo'];
111 var el = $(item).parent();
112 $(el).append(this.template(this.model.toJSON()));
113 }
114 else{
115 console.log(this.model.toJSON());
116 $(this.el).append(this.template(this.model.toJSON()));
117 }
102118 }
103119 else {
104 $(this.el).append(this.edit_template(model.toJSON()));
120 $(this.el).append(this.edit_template(this.model.toJSON()));
105121 }
122 return this;
106123
107
108124 },
109125
110126 save: function(e) {
111127 /* Create a sweet and send it to the sweet store.
112128 Update the view to include the comment */
113129 e.preventDefault();
114 this.model.set({'how':{'comment':this.$("textarea.form-control").val(),
115 'replyTo':this.model.get('how')['replyTo']}});
130 this.model.set({'how': new How({'comment':this.$("textarea.form-control").val()})});
116131 this.model.set({created: new Date().toUTCString().substr(0, 25)});
117132 new LoginView({model:this.model});
118133
119134 },
120135 reply: function(e) {
136 e.stopPropagation();
121137 e.preventDefault();
122 var rep = new Comment();
123 rep.set({'how':{'replyTo':$(e.currentTarget).attr('for'),
124 'comment':''}});
138 var rep = new Comment({'how': new How({'replyTo':$(e.currentTarget).attr('for')})});
125139 C.comments.add(rep);
126140 }
127141 });
156156 var password = $("#password").val();
157157
158158 $.ajax({
159 url: "http://127.0.0.1:5001/authenticate",
159 url: C.url + C.auth,
160160 type: 'POST',
161161 data: {user: username, hash: password},
162162 context: this.model,
170170 });
171171 }});
172172 }
173 });
174
175 var AppView = Backbone.View.extend({
176 el: $("#comments"),
177 initialize: function() {
178 this.listenTo(C.comments, "add", this.showOne);
179
180 },
181 showOne: function(model) {
182 model.set({'how': new How(model.get('how'))});
183 var view = new CommentView({model:model});
184 $(this.el).append(view.render().el);
185 }
186
173187 });
174188
175189})(C);