Commit fab281d70ddf1752dc271f61a4aa8ea51b232f4f
- Diff rendering mode:
- inline
- side by side
app.js
(24 / 26)
  | |||
1 | (function(C){ | ||
1 | (function(C) { | ||
2 | 2 | ||
3 | C.init = function(){ | ||
3 | C.init = function() { | ||
4 | 4 | // C.CommentView = CommentView; //Expose the API to window. | |
5 | 5 | // C.Comment = Comment; | |
6 | 6 | /* Create a empty model and view */ | |
… | … | ||
10 | 10 | }; | |
11 | 11 | var Comment = Backbone.Model.extend({ | |
12 | 12 | /* A model representing a comment. */ | |
13 | defaults: function(){ | ||
13 | defaults: function() { | ||
14 | 14 | return { | |
15 | 15 | 'who':'', | |
16 | 16 | 'what': C.what, | |
… | … | ||
22 | 22 | }; | |
23 | 23 | }, | |
24 | 24 | url: "http://127.0.0.1:5001/sweets", | |
25 | initialize: function(){ | ||
25 | initialize: function() { | ||
26 | 26 | this.set({"what": C.what}); | |
27 | 27 | } | |
28 | 28 | }); | |
… | … | ||
30 | 30 | var Comments = Backbone.Collection.extend({ | |
31 | 31 | model: Comment, | |
32 | 32 | url: C.url + C.sweet, | |
33 | initialize: function(){ | ||
33 | initialize: function() { | ||
34 | 34 | this.getAll({ | |
35 | 35 | "what":C.what, | |
36 | "success": function(data){ | ||
36 | "success": function(data) { | ||
37 | 37 | C.comments.add(data); | |
38 | 38 | ||
39 | 39 | } | |
40 | 40 | }); | |
41 | 41 | }, | |
42 | getAll: function(options){ | ||
42 | getAll: function(options) { | ||
43 | 43 | /* Get the previous comments */ | |
44 | 44 | if(!options.what) { | |
45 | 45 | throw Error('"what" option must be passed to get sweets of a URI'); | |
… | … | ||
76 | 76 | events:{ | |
77 | 77 | "click .save": "save" | |
78 | 78 | }, | |
79 | initialize: function(){ | ||
79 | initialize: function() { | ||
80 | 80 | this.render(); | |
81 | 81 | }, | |
82 | 82 | ||
83 | render: function(el){ | ||
83 | render: function(el) { | ||
84 | 84 | $(this.el).append(this.template(this.model.toJSON())); | |
85 | 85 | }, | |
86 | 86 | ||
87 | save: function(e){ | ||
87 | save: function(e) { | ||
88 | 88 | /* Create a sweet and send it to the sweet store. | |
89 | 89 | Update the view to include the comment */ | |
90 | 90 | e.preventDefault(); | |
… | … | ||
102 | 102 | "click button": "reply" | |
103 | 103 | }, | |
104 | 104 | template: _.template($("#commented-template").html()), | |
105 | initialize: function(){ | ||
105 | initialize: function() { | ||
106 | 106 | this.listenTo(this.collection, "add", this.render); | |
107 | 107 | this.render(); | |
108 | 108 | }, | |
109 | render: function(el){ | ||
110 | _.each(this.collection.models, function(comment){ | ||
111 | t = _.template($("#commented-template").html()); | ||
112 | $("#commented").append(t(comment.toJSON())); | ||
113 | }); | ||
109 | render: function(el) { | ||
110 | _.each(this.collection.models, function(comment) { | ||
111 | console.log(comment); | ||
112 | $(this.el).append(this.template(comment.toJSON())); | ||
113 | }, this); | ||
114 | 114 | ||
115 | 115 | }, | |
116 | reply: function(e){ | ||
116 | reply: function(e) { | ||
117 | 117 | var rep = new Comment({'how':{'replyTo':$(e.currentTarget).attr('for')}}); | |
118 | 118 | $(e.currentTarget).parent().after("<div class='comment-reply'></div>"); | |
119 | el = $("#commented .comment-reply"); | ||
119 | var el = $("#commented .comment-reply"); | ||
120 | 120 | new CommentView({model: rep, el:el }); | |
121 | 121 | } | |
122 | 122 | ||
… | … | ||
126 | 126 | events:{ | |
127 | 127 | "click #saveButton": "login" | |
128 | 128 | }, | |
129 | initialize: function(){ | ||
129 | initialize: function() { | ||
130 | 130 | this.render(); | |
131 | 131 | }, | |
132 | render: function(){ | ||
132 | render: function() { | ||
133 | 133 | $(this.el).modal(); | |
134 | 134 | }, | |
135 | login: function(model){ | ||
135 | login: function(model) { | ||
136 | 136 | var username = $("#username").val(); | |
137 | 137 | var password = $("#password").val(); | |
138 | 138 | ||
… | … | ||
144 | 144 | success: function(data) { | |
145 | 145 | this.set({"who":username}); | |
146 | 146 | $(".modal").modal('toggle'); | |
147 | this.save(null,{success:function(model){ | ||
147 | this.save(null,{success:function(model) { | ||
148 | 148 | C.comments.add(model); | |
149 | 149 | $("textarea.form-control").val(""); //Reset the view to have no content. | |
150 | 150 | } | |
151 | }); | ||
152 | |||
153 | }}); | ||
154 | |||
151 | }); | ||
152 | }}); | ||
155 | 153 | } | |
156 | 154 | }); | |
157 | 155 |