PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/backbone/backbone.md

https://github.com/jondkoon/BackboneTalk
Markdown | 240 lines | 179 code | 61 blank | 0 comment | 0 complexity | 7b5a4779172e1c4b4dcc4de76c6628d9 MD5 | raw file
  1. #Backbone Talk
  2. ##July 26th 2012
  3. <br>
  4. <br>
  5. <br>
  6. <br>
  7. ![Backbone.js](imgs/backbone.png)
  8. ---
  9. #What is Backbone
  10. Backbone.js gives structure to web applications by providing **models** with key-value binding and custom events, **collections** with a rich API of enumerable functions, **views** with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.
  11. ---
  12. ##A Simple Backbone Model
  13. !javascript
  14. var Person = Backbone.Model.extend({
  15. defaults: {
  16. Name: null,
  17. Age: null
  18. }
  19. });
  20. var elliot = new Person({
  21. Name: "Elliot",
  22. Age: 10/12
  23. });
  24. print(elliot.get("Name"));
  25. ---
  26. ##Model Attributes
  27. - In order to distinguish methods from attributes of the model backbone stores all attributes in the **attributes** property
  28. - The attributes property is what is JSONified and sent to the server when you do `model.save()`
  29. .
  30. !javascript
  31. printJSON(elliot.attributes);
  32. ---
  33. ##Encapsulation
  34. It is not best practice to access or modify attributes of a model directly
  35. !javascript
  36. print(elliot.get("Name"));
  37. //set can be passed an object
  38. elliot.set({
  39. Name: "Elliot Thomas",
  40. Age: 1
  41. });
  42. print(elliot.get("Name"));
  43. //or a key value pair
  44. elliot.set("Name","Elliot Thomas Koon");
  45. print(elliot.get("Name"));
  46. ---
  47. ##Model Events
  48. When you perform an attribute change on a backbone model it fires change events to notify interested parties of the change
  49. !javascript
  50. elliot.on("change:Age",function(model, age){
  51. print("Happy Birthday!\n You are " +
  52. age + " years old\n");
  53. });
  54. elliot.set("Age", 2);
  55. elliot.set("Age", 3);
  56. ---
  57. ##Persistence
  58. - If you provide a model with a url then you can utilize several helper function to communicate with the backend
  59. - This is usually done in the model constructor
  60. .
  61. !javascript
  62. var Person = Backbone.Model.extend({
  63. url : "/api/kids"
  64. defaults: {
  65. Name: null,
  66. Age: null
  67. }
  68. });
  69. ---
  70. ##Save
  71. - When using `model.save()` an ajax request is made to the specified url
  72. - If the model is `isNew()`, it was created on the client, then backbone will make a request to the url with a HTTP `POST`. For example `/api/kids`
  73. - If the model is `!isNew()`, it has been saved or was provided by the server, then a HTTP `PUT` will be used and the models `id` will be appended to the url. For example `/api/kids/1`
  74. - If a model does not have a url of its own, but is part of a collection the url of the collection will be used
  75. ---
  76. #Destroy
  77. - backbone provides a `destroy()` method that works similiarly to save except that an HTTP `DELETE` is sent to the specified url
  78. - If the model `isNew()` then destroy will not make a request to the server
  79. - When a model is destroyed it fires a destroy event
  80. .
  81. !javascript
  82. var workItem = new WorkItem({
  83. Name: "my work item"
  84. });
  85. workItem.on("destroy", function(model){
  86. print('"' + model.get("Name") +
  87. '"\n' + " has been destroyed");
  88. });
  89. workItem.destroy();
  90. ---
  91. #Persistence Options
  92. Backbones persistence methods `save` and `destroy` accepts `success` and `error` callbacks
  93. For example:
  94. !javascript
  95. var WorkItem = Backbone.Model.extend({
  96. url: "api/workItems",
  97. defaults: {
  98. Name: null
  99. }
  100. });
  101. var workItem = new WorkItem({
  102. Name: "my work item"
  103. });
  104. workItem.save({
  105. success: function(){
  106. alert("saved");
  107. },
  108. error: function(){
  109. alert("error");
  110. }
  111. });
  112. ---
  113. #Collections
  114. - Backbone has a construct called `Collections` that is a group of models
  115. - Collections have methods such as `add`, `remove`, and `get`
  116. .
  117. !javascript
  118. var WorkItemCollection = Backbone.Collection.extend({
  119. model: WorkItem
  120. });
  121. var workItems = new WorkItemCollection();
  122. workItems.add(
  123. new WorkItem({
  124. Name: "workitem 1"
  125. })
  126. );
  127. print("");
  128. print("");
  129. printJSON(workItems.models);
  130. ---
  131. #Collection Add Event
  132. - Backbone collections have several events that are triggered including `add` and `remove`
  133. .
  134. !javascript
  135. var workItems = new WorkItemCollection();
  136. workItems.on("add", function(model){
  137. printJSON(model);
  138. });
  139. var workItem1 = new WorkItem({
  140. Name: "workitem 1"
  141. });
  142. workItems.add(workItem1);
  143. workItems.add(
  144. new WorkItem({
  145. Name: "workitem 2"
  146. })
  147. );
  148. //For next slide
  149. window.workItem1 = workItem1;
  150. window.workItems = workItems;
  151. ---
  152. #Collection Remove Event
  153. !javascript
  154. workItems.on("remove", function(model){
  155. print("removed:");
  156. printJSON(model);
  157. });
  158. workItems.remove(workItem1);
  159. ---
  160. #Backbone Views
  161. From the Backbone docs:
  162. > The general idea is to organize your interface into logical views, backed by models, each of which can be updated independently when the model changes, without having to redraw the page. Instead of digging into a JSON object, looking up an element in the DOM, and updating the HTML by hand, you can bind your view's render function to the model's "change" event — and now everywhere that model data is displayed in the UI, it is always immediately up to date.
  163. ---
  164. #A backbone view example
  165. <ul class="work-items">
  166. <li>work item1</ul>
  167. </ul>
  168. !javascript
  169. var WorkItemView = new Backbone.View.extend({
  170. tagName: "li",
  171. render: function(){
  172. this.$el.html(this.model.get("Name"));
  173. }
  174. });
  175. var workItem2 = new WorkItem({Name: "work item2"});
  176. var workItemView = new WorkItemView({
  177. model: workItem2
  178. });
  179. $('.work-items').append(workItemView.render());