Commit 04018d7bc0f7f83eb18463a5204ffe9128987354
- Diff rendering mode:
- inline
- side by side
app.js
(70 / 30)
  | |||
1 | 1 | (function(C) { | |
2 | 2 | ||
3 | 3 | C.init = function() { | |
4 | // C.CommentView = CommentView; //Expose the API to window. | ||
5 | // C.Comment = Comment; | ||
6 | 4 | /* 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 | |||
11 | 12 | }; | |
13 | var How = Backbone.Model.extend({ | ||
14 | defaults: { | ||
15 | 'comment':'', | ||
16 | 'replyTo':'' | ||
17 | }, | ||
18 | initialize: function() { | ||
19 | } | ||
20 | }); | ||
12 | 21 | var Comment = Backbone.Model.extend({ | |
13 | 22 | /* A model representing a comment. */ | |
23 | url: function() { | ||
24 | return C.url + C.sweet; | ||
25 | }, | ||
14 | 26 | defaults: function() { | |
15 | 27 | return { | |
16 | 28 | 'who':'', | |
17 | 'what': C.what, | ||
29 | 'what':C.what, | ||
18 | 30 | 'where': window.location.href, | |
19 | 'how':{ | ||
20 | 'comment':'', | ||
21 | 'replyTo':'' | ||
22 | } | ||
31 | 'how': '' | ||
23 | 32 | }; | |
24 | 33 | }, | |
25 | url: "http://127.0.0.1:5001/sweets", | ||
34 | |||
26 | 35 | initialize: function() { | |
27 | this.set({"what": C.what}); | ||
36 | }, | ||
37 | toJSON: function() { | ||
38 | how = this.get('how').toJSON(); | ||
39 | return this; | ||
28 | 40 | } | |
29 | 41 | }); | |
30 | 42 | ||
31 | var Comments = Backbone.Collection.extend({ | ||
43 | var Sweets = Backbone.Collection.extend({ | ||
32 | 44 | model: Comment, | |
33 | 45 | url: C.url + C.sweet, | |
34 | 46 | initialize: function() { | |
… | … | ||
83 | 83 | ||
84 | 84 | }); | |
85 | 85 | ||
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 | // }); | ||
86 | 95 | var CommentView = Backbone.View.extend({ | |
87 | el: $("#comments"), | ||
96 | tagName: 'div', | ||
88 | 97 | template: _.template($("#commented-template").html()), | |
89 | 98 | edit_template: _.template($("#comment-template").html()), | |
90 | 99 | events:{ | |
91 | 100 | "click .save": "save", | |
92 | 101 | "click .comment button": "reply" | |
93 | 102 | }, | |
94 | initialize: function() { | ||
95 | this.listenTo(this.collection, "add", this.render); | ||
96 | }, | ||
103 | intialize: function() { | ||
104 | this.listenTo(this.model, "save", this.render); | ||
97 | 105 | ||
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 | } | ||
102 | 118 | } | |
103 | 119 | else { | |
104 | $(this.el).append(this.edit_template(model.toJSON())); | ||
120 | $(this.el).append(this.edit_template(this.model.toJSON())); | ||
105 | 121 | } | |
122 | return this; | ||
106 | 123 | ||
107 | |||
108 | 124 | }, | |
109 | 125 | ||
110 | 126 | save: function(e) { | |
111 | 127 | /* Create a sweet and send it to the sweet store. | |
112 | 128 | Update the view to include the comment */ | |
113 | 129 | 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()})}); | ||
116 | 131 | this.model.set({created: new Date().toUTCString().substr(0, 25)}); | |
117 | 132 | new LoginView({model:this.model}); | |
118 | 133 | ||
119 | 134 | }, | |
120 | 135 | reply: function(e) { | |
136 | e.stopPropagation(); | ||
121 | 137 | 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')})}); | ||
125 | 139 | C.comments.add(rep); | |
126 | 140 | } | |
127 | 141 | }); | |
… | … | ||
156 | 156 | var password = $("#password").val(); | |
157 | 157 | ||
158 | 158 | $.ajax({ | |
159 | url: "http://127.0.0.1:5001/authenticate", | ||
159 | url: C.url + C.auth, | ||
160 | 160 | type: 'POST', | |
161 | 161 | data: {user: username, hash: password}, | |
162 | 162 | context: this.model, | |
… | … | ||
170 | 170 | }); | |
171 | 171 | }}); | |
172 | 172 | } | |
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 | |||
173 | 187 | }); | |
174 | 188 | ||
175 | 189 | })(C); |