From fab281d70ddf1752dc271f61a4aa8ea51b232f4f Mon Sep 17 00:00:00 2001 From: Arvind Date: Mon, 3 Mar 2014 15:58:20 +0530 Subject: [PATCH] Fix: Code refactor. Following code conventions mentioned at http://trac.pantoto.org/general/wiki/CodeConventions --- app.js | 50 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/app.js b/app.js index ef0ee29..abdbe9c 100644 --- a/app.js +++ b/app.js @@ -1,6 +1,6 @@ -(function(C){ +(function(C) { - C.init = function(){ + C.init = function() { // C.CommentView = CommentView; //Expose the API to window. // C.Comment = Comment; /* Create a empty model and view */ @@ -10,7 +10,7 @@ }; var Comment = Backbone.Model.extend({ /* A model representing a comment. */ - defaults: function(){ + defaults: function() { return { 'who':'', 'what': C.what, @@ -22,7 +22,7 @@ }; }, url: "http://127.0.0.1:5001/sweets", - initialize: function(){ + initialize: function() { this.set({"what": C.what}); } }); @@ -30,16 +30,16 @@ var Comments = Backbone.Collection.extend({ model: Comment, url: C.url + C.sweet, - initialize: function(){ + initialize: function() { this.getAll({ "what":C.what, - "success": function(data){ + "success": function(data) { C.comments.add(data); } }); }, - getAll: function(options){ + getAll: function(options) { /* Get the previous comments */ if(!options.what) { throw Error('"what" option must be passed to get sweets of a URI'); @@ -76,15 +76,15 @@ events:{ "click .save": "save" }, - initialize: function(){ + initialize: function() { this.render(); }, - render: function(el){ + render: function(el) { $(this.el).append(this.template(this.model.toJSON())); }, - save: function(e){ + save: function(e) { /* Create a sweet and send it to the sweet store. Update the view to include the comment */ e.preventDefault(); @@ -102,21 +102,21 @@ "click button": "reply" }, template: _.template($("#commented-template").html()), - initialize: function(){ + initialize: function() { this.listenTo(this.collection, "add", this.render); this.render(); }, - render: function(el){ - _.each(this.collection.models, function(comment){ - t = _.template($("#commented-template").html()); - $("#commented").append(t(comment.toJSON())); - }); + render: function(el) { + _.each(this.collection.models, function(comment) { + console.log(comment); + $(this.el).append(this.template(comment.toJSON())); + }, this); }, - reply: function(e){ + reply: function(e) { var rep = new Comment({'how':{'replyTo':$(e.currentTarget).attr('for')}}); $(e.currentTarget).parent().after("
"); - el = $("#commented .comment-reply"); + var el = $("#commented .comment-reply"); new CommentView({model: rep, el:el }); } @@ -126,13 +126,13 @@ events:{ "click #saveButton": "login" }, - initialize: function(){ + initialize: function() { this.render(); }, - render: function(){ + render: function() { $(this.el).modal(); }, - login: function(model){ + login: function(model) { var username = $("#username").val(); var password = $("#password").val(); @@ -144,14 +144,12 @@ success: function(data) { this.set({"who":username}); $(".modal").modal('toggle'); - this.save(null,{success:function(model){ + this.save(null,{success:function(model) { C.comments.add(model); $("textarea.form-control").val(""); //Reset the view to have no content. } - }); - - }}); - + }); + }}); } }); -- 1.7.10.4