PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/_posts/2011-01-26-what-is-a-collection.textile

https://github.com/ajcubeta/backbonetutorials
Unknown | 85 lines | 57 code | 28 blank | 0 comment | 0 complexity | efef675cbf0fe9485a12178963cdab7b MD5 | raw file
  1. ---
  2. layout: post
  3. title: What is a collection?
  4. type: beginner
  5. posturl: http://backbonetutorials.com/what-would-you-use-backbone
  6. ---
  7. h1. In progress
  8. h2. What is a collection?
  9. p. Backbone collections are simply an ordered set of "models":/what-is-a-model. Such that it can be used in situations such as;
  10. * Model: Student, Collection: ClassStudents
  11. * Model: Todo Item, Collection: Todo List
  12. * Model: Animals, Collection: Zoo
  13. Typically your collection will only use one type of model but models themselves are not limited to a type of collection;
  14. * Model: Student, Collection: Gym Class
  15. * Model: Student, Collection: Art Class
  16. * Model: Student, Collection: English Class
  17. Here is a generic Model/Collection example.
  18. {% highlight javascript %}
  19. var Song = Backbone.Model.extend({
  20. initialize: function(){
  21. console.log("Music is the answer");
  22. }
  23. });
  24. var Album = Backbone.Collection.extend({
  25. model: Song
  26. });
  27. {% endhighlight %}
  28. h3. Building a collection
  29. p. Now we are going to populate a creation with some useful data.
  30. {% highlight javascript %}
  31. var Song = Backbone.Model.extend({
  32. defaults: {
  33. name: "Not specified",
  34. artist: "Not specified"
  35. },
  36. initialize: function(){
  37. console.log("Music is the answer");
  38. }
  39. });
  40. var Album = Backbone.Collection.extend({
  41. model: Song
  42. });
  43. var song1 = new Song({ name: "How Bizarre", artist: "OMC" });
  44. var song2 = new Song({ name: "Sexual Healing", artist: "Marvin Gaye" });
  45. var song3 = new Song({ name: "Talk It Over In Bed", artist: "OMC" });
  46. var myAlbum = new Album([ song1, song2, song3]);
  47. console.log( myAlbum.models ); // [song1, song2, song3]
  48. {% endhighlight %}
  49. h3. So how does Backbone.js help?
  50. p. Backbone is an incredibly small library for the amount of functionality and structure it gives you. One can not easily summarize the benefits you will reap from using it. If you read through some of the beginner tutorials the benefits will soon become self evident and due to Backbone.js light nature you can incrementally include it in any current or future projects.
  51. h3. Relevant Links
  52. * "Backbone.js official website":http://documentcloud.github.com/backbone/
  53. * "great hackernews discussion /w post from author":http://news.ycombinator.com/item?id=2119704
  54. h3. Author
  55. * "Thomas Davis":https://github.com/thomasdavis
  56. h3. Contributors
  57. * "FND":https://github.com/FND