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