Commit 3a09777d2a4be65bf3fa6a56bfad8402b2a19152

  • avatar
  • arvind
  • Sun Mar 02 13:51:33 IST 2014
Feature completion.  Authenticate to sweet store, and get all previous
comments from sweet store.

Reply to a comment is not completely functional yet.  POST to sweet
store is failing.
app.js
(114 / 15)
  
44 // C.CommentView = CommentView; //Expose the API to window.
55 // C.Comment = Comment;
66 /* Create a empty model and view */
7 var cv = new CommentView({model: new Comment()});
7 C.comments = new Comments();
8 C.cv = new PostedCommentView({collection: C.comments});
9 new CommentView({model: new Comment()});
810 };
911 var Comment = Backbone.Model.extend({
1012 /* A model representing a comment. */
11 defaults:{
12 what:C.config.what,
13 where: window.location.href
13 defaults: function(){
14 return {
15 what: C.what,
16 where: window.location.href,
17 how:{
18 comment:''
19 }
20 };
1421 },
1522 url: "http://127.0.0.1:5001/sweets",
1623 initialize: function(){
24 this.set({"what": C.what});
25 }
26 });
1727
28 var Comments = Backbone.Collection.extend({
29 model: Comment,
30 url: C.url + C.sweet,
31 initialize: function(){
32 this.getAll({
33 "what":C.what,
34 "success": function(data){
35 C.comments.add(data);
36
37 }
38 });
39 },
40 getAll: function(options){
41 /* Get the previous comments */
42 if(!options.what) {
43 throw Error('"what" option must be passed to get sweets of a URI');
44 return false;
45 }
46 // setting up params
47 var what = options.what,
48 who = options.who || null;
49 url = C.url + C.get + '?what=' + what;
50 if(who) {
51 url += '&who=' + who;
52 }
53 // get them!
54 this.sync('read', this, {
55 url: url,
56 success: function() {
57 if(typeof options.success === 'function') {
58 options.success.apply(this, arguments);
59 }
60 },
61 error: function() {
62 if(typeof options.error === 'function') {
63 options.error.apply(this, arguments);
64 }
65 }
66 });
1867 }
68
1969 });
2070
2171 var CommentView = Backbone.View.extend({
7777 initialize: function(){
7878 // _.bindAll.apply(_, [this].concat(_.functions(this)));
7979 // _.bind(this.render, this);
80// this.listenTo(this.model, "change", this.render);
80 // this.listenTo(this.model, "change", this.render);
8181 this.render();
8282 },
8383
8484 render: function(el){
85 $(this.el).append(this.template());
85 $(this.el).append(this.template(this.model.toJSON()));
8686 },
8787
88 save: function(){
88 save: function(e){
8989 /* Create a sweet and send it to the sweet store.
9090 Update the view to include the comment */
91 this.model.set({"how":{"comment":$("textarea.form-control").val()}});
91 this.model.set({how:{comment:this.$("textarea.form-control").val(),
92 replyTo:this.model.get('how')['replyTo']}});
9293 this.model.set({created: new Date().toUTCString().substr(0, 25)});
93 this.model.save(null,{success:function(model){
94 new PostedCommentView({model:new
95 Comment(_.clone(model.attributes)[0])});
96 $("textarea.form-control").val(""); //Reset the view to have no content.
97 }});
94 new LoginView({model:this.model});
9895
9996 }
10097 });
10198
10299 var PostedCommentView = Backbone.View.extend({
103100 el: "#commented",
101 events: {
102 "click button": "reply"
103 },
104104 template: _.template($("#commented-template").html()),
105105 initialize: function(){
106 this.listenTo(this.collection, "add", this.render);
106107 this.render();
107108 },
108109 render: function(el){
109 content = this.model.toJSON()["how"]["comment"];
110 $(this.el).append(this.template({content:content}));
110 _.each(this.collection.models, function(comment){
111 t = _.template($("#commented-template").html());
112 $("#commented").append(t(comment.toJSON()));
113 //comment.get("how").comment);
114 });
115
116 // content = this.model.toJSON()["how"]["comment"];
117 // $(this.el).append(this.template({content:content}));
118 },
119 reply: function(e){
120 var rep = new Comment({how:{replyTo:$(e.currentTarget).attr('for')}});
121 $(e.currentTarget).parent().after("<div class='comment-reply'></div>");
122 el = $("#commented .comment-reply");
123 new CommentView({model: rep, el:el });
111124 }
112125
113126 });
127 var LoginView = Backbone.View.extend({
128 el: ".modal",
129 events:{
130 "click #saveButton": "login"
131 },
132 initialize: function(){
133 this.render();
134 },
135 render: function(){
136 $(this.el).modal();
137 },
138 login: function(model){
139 var username = $("#username").val();
140 var password = $("#password").val();
141
142 $.ajax({
143 url: "http://127.0.0.1:5001/authenticate",
144 type: 'POST',
145 data: {user: username, hash: password},
146 context: this.model,
147 success: function(data) {
148 this.set({"who":username});
149 $(".modal").modal('toggle');
150 this.save(null,{success:function(model){
151 C.comments.add(model);
152 $("textarea.form-control").val(""); //Reset the view to have no content.
153 }
154 });
155
156 }}).then(function(){
157
158 });
159
160 }
161 });
162
114163})(C);