Commit fab281d70ddf1752dc271f61a4aa8ea51b232f4f

  • avatar
  • arvind
  • Mon Mar 03 15:58:20 IST 2014
Fix:
    Code refactor.
    Following code conventions mentioned at
    http://trac.pantoto.org/general/wiki/CodeConventions
app.js
(24 / 26)
  
1(function(C){
1(function(C) {
22
3 C.init = function(){
3 C.init = function() {
44 // C.CommentView = CommentView; //Expose the API to window.
55 // C.Comment = Comment;
66 /* Create a empty model and view */
1010 };
1111 var Comment = Backbone.Model.extend({
1212 /* A model representing a comment. */
13 defaults: function(){
13 defaults: function() {
1414 return {
1515 'who':'',
1616 'what': C.what,
2222 };
2323 },
2424 url: "http://127.0.0.1:5001/sweets",
25 initialize: function(){
25 initialize: function() {
2626 this.set({"what": C.what});
2727 }
2828 });
3030 var Comments = Backbone.Collection.extend({
3131 model: Comment,
3232 url: C.url + C.sweet,
33 initialize: function(){
33 initialize: function() {
3434 this.getAll({
3535 "what":C.what,
36 "success": function(data){
36 "success": function(data) {
3737 C.comments.add(data);
3838
3939 }
4040 });
4141 },
42 getAll: function(options){
42 getAll: function(options) {
4343 /* Get the previous comments */
4444 if(!options.what) {
4545 throw Error('"what" option must be passed to get sweets of a URI');
7676 events:{
7777 "click .save": "save"
7878 },
79 initialize: function(){
79 initialize: function() {
8080 this.render();
8181 },
8282
83 render: function(el){
83 render: function(el) {
8484 $(this.el).append(this.template(this.model.toJSON()));
8585 },
8686
87 save: function(e){
87 save: function(e) {
8888 /* Create a sweet and send it to the sweet store.
8989 Update the view to include the comment */
9090 e.preventDefault();
102102 "click button": "reply"
103103 },
104104 template: _.template($("#commented-template").html()),
105 initialize: function(){
105 initialize: function() {
106106 this.listenTo(this.collection, "add", this.render);
107107 this.render();
108108 },
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);
114114
115115 },
116 reply: function(e){
116 reply: function(e) {
117117 var rep = new Comment({'how':{'replyTo':$(e.currentTarget).attr('for')}});
118118 $(e.currentTarget).parent().after("<div class='comment-reply'></div>");
119 el = $("#commented .comment-reply");
119 var el = $("#commented .comment-reply");
120120 new CommentView({model: rep, el:el });
121121 }
122122
126126 events:{
127127 "click #saveButton": "login"
128128 },
129 initialize: function(){
129 initialize: function() {
130130 this.render();
131131 },
132 render: function(){
132 render: function() {
133133 $(this.el).modal();
134134 },
135 login: function(model){
135 login: function(model) {
136136 var username = $("#username").val();
137137 var password = $("#password").val();
138138
144144 success: function(data) {
145145 this.set({"who":username});
146146 $(".modal").modal('toggle');
147 this.save(null,{success:function(model){
147 this.save(null,{success:function(model) {
148148 C.comments.add(model);
149149 $("textarea.form-control").val(""); //Reset the view to have no content.
150150 }
151 });
152
153 }});
154
151 });
152 }});
155153 }
156154 });
157155