/comments_example/_attachments/js/app.js

https://github.com/carljmosca/backbone-couchdb · JavaScript · 164 lines · 107 code · 26 blank · 31 comment · 5 complexity · 69d37f40548e643c94d58894f71e68c1 MD5 · raw file

  1. $(function(){
  2. // Fill this with your database information.
  3. // `ddoc_name` is the name of your couchapp project.
  4. Backbone.couch_connector.config.db_name = "backbone-couchapp";
  5. Backbone.couch_connector.config.ddoc_name = "backbone_couchapp_comments";
  6. // If set to true, the connector will listen to the changes feed
  7. // and will provide your models with real time remote updates.
  8. Backbone.couch_connector.config.global_changes = true;
  9. // Enables Mustache.js-like templating.
  10. _.templateSettings = {
  11. interpolate : /\{\{(.+?)\}\}/g
  12. };
  13. // The model for a comment is kinda simple.
  14. // We only need a name, a text and a date.
  15. var CommentModel = Backbone.Model.extend({
  16. initialize : function(){
  17. if(!this.get("name")){
  18. this.set({"name": "Anonymus"});
  19. }
  20. if(!this.get("text")){
  21. this.set({"text": "Nothing"});
  22. }
  23. if(!this.get("date")){
  24. this.set({"date": new Date().getTime()});
  25. }
  26. }
  27. });
  28. // Now let's define a new Collection of Comments
  29. var CommentList = Backbone.Collection.extend({
  30. // The couchdb-connector is capable of mapping the url scheme
  31. // proposed by the authors of Backbone to documents in your database,
  32. // so that you don't have to change existing apps when you switch the sync-strategy
  33. url : "/comments",
  34. model : CommentModel,
  35. // The comments should be ordered by date
  36. comparator : function(comment){
  37. return comment.get("date");
  38. }
  39. });
  40. var Comments = new CommentList();
  41. // This view is responsible for creating the Comment fields.
  42. var EditView = Backbone.View.extend({
  43. el : $("#edit"),
  44. events : {
  45. "click #send" : "onSubmit"
  46. },
  47. initialize : function(){
  48. _.bindAll(this, "onSubmit");
  49. },
  50. // Simply takes the vals from the input fields and
  51. // creates a new Comment.
  52. onSubmit : function(){
  53. var name = $("#name").val();
  54. var text = $("#text").val();
  55. // sanitize user input...you never know ;)
  56. name = name.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/"/g, '&quot;');
  57. text = text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/"/g, '&quot;');
  58. Comments.create({
  59. "name" : name,
  60. "text" : text,
  61. "date" : new Date().getTime()
  62. });
  63. }
  64. });
  65. // Represents an comment entry
  66. var EntryView = Backbone.View.extend({
  67. tagName : "tr",
  68. template : _.template($("#entry-template").html()),
  69. // Clicking the `X` leads to a deletion
  70. events : {
  71. "click .delete" : "deleteMe",
  72. "dblclick td" : "dummyFetch"
  73. },
  74. // If there's a change in our model, rerender it
  75. initialize : function(){
  76. _.bindAll(this, 'render', 'deleteMe', 'dummyFetch');
  77. this.model.bind('change', this.render);
  78. },
  79. dummyFetch : function(){
  80. // Fetch the state of the model from the server.
  81. // Used this to test the model sync.
  82. this.model.fetch();
  83. },
  84. render : function(){
  85. var content = this.model.toJSON();
  86. $(this.el).html(this.template(content));
  87. return this;
  88. },
  89. // Fade out the element and destroy the model
  90. deleteMe : function(){
  91. if(this.model)
  92. this.model.destroy();
  93. $(this.el).fadeOut("fast",function(){
  94. $(this).remove();
  95. });
  96. }
  97. });
  98. // The view for all comments
  99. var CommentsTable = Backbone.View.extend({
  100. el: $("#comments"),
  101. initialize : function(){
  102. _.bindAll(this, 'refreshed', 'addRow', 'deleted');
  103. Comments.bind("reset", this.refreshed);
  104. Comments.bind("add", this.addRow);
  105. Comments.bind("remove", this.deleted);
  106. },
  107. // Prepends an entry row
  108. addRow : function(comment){
  109. var view = new EntryView({model: comment});
  110. var rendered = view.render().el;
  111. this.el.prepend(rendered);
  112. },
  113. // Renders all comments into the table
  114. refreshed : function(){
  115. // reset the table
  116. $("#comments").html("");
  117. if(Comments.length > 0){
  118. // add each element
  119. Comments.each(this.addRow);
  120. }
  121. },
  122. // A comment has been deleted, so we rerender the table,
  123. // because this update could also come from another user via the
  124. // _changes feed
  125. deleted : function(){
  126. this.refreshed();
  127. }
  128. });
  129. // The App controller initializes the app by calling `Comments.fetch()`
  130. var App = Backbone.Router.extend({
  131. initialize : function(){
  132. Comments.fetch();
  133. }
  134. });
  135. new EditView();
  136. new CommentsTable();
  137. new App();
  138. });