From a4326081d72670cb49fd61e30659b47e335d5075 Mon Sep 17 00:00:00 2001 From: Arvind Date: Sat, 1 Mar 2014 14:01:54 +0530 Subject: [PATCH] Comments: Basic version of a commenting application. Uses sweets to publish a comment. Lacks authentication and CAPTCHA for SPAM protection. --- app.js | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 app.js diff --git a/app.js b/app.js new file mode 100644 index 0000000..2ece6a0 --- /dev/null +++ b/app.js @@ -0,0 +1,64 @@ +(function(C){ + + C.init = function(){ + // C.CommentView = CommentView; //Expose the API to window. + // C.Comment = Comment; + /* Create a empty model and view */ + var cv = new CommentView({model: new Comment()}); + }; + var Comment = Backbone.Model.extend({ + /* A model representing a comment. */ + defaults:{ + what:C.config.what, + where: window.location.href + }, + url: "http://127.0.0.1:5001/sweets", + initialize: function(){ + + } + }); + + var CommentView = Backbone.View.extend({ + el: $("#comments"), + template: _.template($("#comment-template").html()), + events:{ + "click .save": "save" + }, + initialize: function(){ + // _.bindAll.apply(_, [this].concat(_.functions(this))); + // _.bind(this.render, this); +// this.listenTo(this.model, "change", this.render); + this.render(); + }, + + render: function(el){ + $(this.el).append(this.template()); + }, + + save: function(){ + /* Create a sweet and send it to the sweet store. + Update the view to include the comment */ + this.model.set({"how":{"comment":$("textarea.form-control").val()}}); + this.model.set({created: new Date().toUTCString().substr(0, 25)}); + this.model.save(null,{success:function(model){ + new PostedCommentView({model:new + Comment(_.clone(model.attributes)[0])}); + $("textarea.form-control").val(""); //Reset the view to have no content. + }}); + + } + }); + + var PostedCommentView = Backbone.View.extend({ + el: "#commented", + template: _.template($("#commented-template").html()), + initialize: function(){ + this.render(); + }, + render: function(el){ + content = this.model.toJSON()["how"]["comment"]; + $(this.el).append(this.template({content:content})); + } + + }); +})(C); -- 1.7.10.4