PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/web/resources/js/Content.js

https://bitbucket.org/anm_brr/write-about-me
JavaScript | 240 lines | 145 code | 42 blank | 53 comment | 11 complexity | d75245173e1dfa7fffa08efb00b31a31 MD5 | raw file
  1. /**
  2. * Created with IntelliJ IDEA.
  3. * User: Rokonoid
  4. * Date: 11/29/12
  5. * Time: 10:38 AM
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. $(function () {
  9. window.MyContent = Backbone.Model.extend({
  10. defaults: {
  11. content: "you have wrote nothing yet, write something...",
  12. done: false
  13. },
  14. initialize: function () {
  15. if (!this.get("content'")) {
  16. this.set({"content": this.defaults.content});
  17. }
  18. },
  19. toggle: function () {
  20. this.save(
  21. { done: !this.get("done")}
  22. );
  23. }
  24. }
  25. );
  26. window.MyContentList = Backbone.Collection.extend({
  27. // Reference to this collection's model.
  28. model: MyContent,
  29. url: "write/about",
  30. // Filter down the list of all myContents items that are finished.
  31. done: function () {
  32. return this.filter(function (myContents) {
  33. return myContents.get('done');
  34. });
  35. },
  36. // Filter down the list to only myContents items that are still not finished.
  37. remaining: function () {
  38. return this.without.apply(this, this.done());
  39. },
  40. // We keep the myContents in sequential order, despite being saved by unordered
  41. // GUID in the database. This generates the next order number for new items.
  42. nextOrder: function () {
  43. if (!this.length) return 1;
  44. return this.last().get('order') + 1;
  45. },
  46. // myContents are sorted by their original insertion order.
  47. comparator: function (myContents) {
  48. return myContents.get('order');
  49. }
  50. });
  51. window.MyContents = new MyContentList;
  52. // MyContentView Item View
  53. // --------------
  54. // The DOM element for a MyContent item...
  55. window.MyContentView = Backbone.View.extend({
  56. //... is a list tag.
  57. tagName: "li",
  58. // Cache the template function for a single item.
  59. template: _.template($('#item-template').html()),
  60. // The DOM events specific to an item.
  61. events: {
  62. "click .check": "toggleDone",
  63. "dblclick div.myConent-content": "edit",
  64. "click span.myConent-destroy": "clear",
  65. "keypress .myConent-input": "updateOnEnter"
  66. },
  67. // The MyContentView listens for changes to its model, re-rendering. Since there's
  68. // a one-to-one correspondence between a **MyContent** and a **MyContentView** in this
  69. // app, we set a direct reference on the model for convenience.
  70. initialize: function () {
  71. this.model.bind('change', this.render, this);
  72. this.model.bind('destroy', this.remove, this);
  73. },
  74. // Re-render the contents of the todo item.
  75. render: function () {
  76. $(this.el).html(this.template(this.model.toJSON()));
  77. this.setContent();
  78. return this;
  79. },
  80. // To avoid XSS (not that it would be harmful in this particular app),
  81. // we use `jQuery.text` to set the contents of the todo item.
  82. setContent: function () {
  83. var content = this.model.get('content');
  84. this.$('.myConent-content').text(content);
  85. this.input = this.$('.myConent-input');
  86. this.input.bind('blur', _.bind(this.close, this));
  87. this.input.val(content);
  88. },
  89. // Toggle the `"done"` state of the model.
  90. toggleDone: function () {
  91. this.model.toggle();
  92. },
  93. // Switch this view into `"editing"` mode, displaying the input field.
  94. edit: function () {
  95. $(this.el).addClass("editing");
  96. this.input.focus();
  97. },
  98. // Close the `"editing"` mode, saving changes to the todo.
  99. close: function () {
  100. this.model.save({content: this.input.val()});
  101. $(this.el).removeClass("editing");
  102. },
  103. // If you hit `enter`, we're through editing the item.
  104. updateOnEnter: function (e) {
  105. if (e.keyCode == 13) this.close();
  106. },
  107. // Remove this view from the DOM.
  108. remove: function () {
  109. $(this.el).remove();
  110. },
  111. // Remove the item, destroy the model.
  112. clear: function () {
  113. this.model.destroy();
  114. }
  115. });
  116. // The Application
  117. // ---------------
  118. // Our overall **AppView** is the top-level piece of UI.
  119. window.AppView = Backbone.View.extend({
  120. // Instead of generating a new element, bind to the existing skeleton of
  121. // the App already present in the HTML.
  122. el: $("#myContentApp"),
  123. // Our template for the line of statistics at the bottom of the app.
  124. statsTemplate: _.template($('#stats-template').html()),
  125. // Delegated events for creating new items, and clearing completed ones.
  126. events: {
  127. "keypress #new-todo": "createOnEnter",
  128. "keyup #new-todo": "showTooltip",
  129. "click .todo-clear a": "clearCompleted"
  130. },
  131. // At initialization we bind to the relevant events on the `Todos`
  132. // collection, when items are added or changed. Kick things off by
  133. // loading any preexisting todos that might be saved in *localStorage*.
  134. initialize: function () {
  135. this.input = this.$("#new-contents");
  136. Todos.bind('add', this.addOne, this);
  137. Todos.bind('reset', this.addAll, this);
  138. Todos.bind('all', this.render, this);
  139. Todos.fetch();
  140. },
  141. // Re-rendering the App just means refreshing the statistics -- the rest
  142. // of the app doesn't change.
  143. render: function () {
  144. this.$('#todo-stats').html(this.statsTemplate({
  145. total: Todos.length,
  146. done: Todos.done().length,
  147. remaining: Todos.remaining().length
  148. }));
  149. },
  150. // Add a single todo item to the list by creating a view for it, and
  151. // appending its element to the `<ul>`.
  152. addOne: function (myContents) {
  153. var view = new MyContentView({model: myContents});
  154. this.$("#myContents-list").append(view.render().el);
  155. },
  156. // Add all items in the **Todos** collection at once.
  157. addAll: function () {
  158. MyContentList.each(this.addOne);
  159. },
  160. // Generate the attributes for a new Todo item.
  161. newAttributes: function () {
  162. return {
  163. content: this.input.val(),
  164. order: MyContentList.nextOrder(),
  165. done: false
  166. };
  167. },
  168. // If you hit return in the main input field, create new **Todo** model,
  169. // persisting it to *localStorage*.
  170. createOnEnter: function (e) {
  171. if (e.keyCode != 13) return;
  172. Todos.create(this.newAttributes());
  173. this.input.val('');
  174. },
  175. // Clear all done todo items, destroying their models.
  176. clearCompleted: function () {
  177. _.each(MyContentList.done(), function (todo) {
  178. todo.destroy();
  179. });
  180. return false;
  181. },
  182. // Lazily show the tooltip that tells you to press `enter` to save
  183. // a new todo item, after one second.
  184. showTooltip: function (e) {
  185. var tooltip = this.$(".ui-tooltip-top");
  186. var val = this.input.val();
  187. tooltip.fadeOut();
  188. if (this.tooltipTimeout) clearTimeout(this.tooltipTimeout);
  189. if (val == '' || val == this.input.attr('placeholder')) return;
  190. var show = function () {
  191. tooltip.show().fadeIn();
  192. };
  193. this.tooltipTimeout = _.delay(show, 1000);
  194. }
  195. });
  196. // Finally, we kick things off by creating the **App**.
  197. window.App = new AppView;
  198. });