PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/cdnsrc/js/controller/DocumentController.js

https://github.com/tauren/Learning-backbone.js
JavaScript | 143 lines | 62 code | 12 blank | 69 comment | 3 complexity | eb86a5e8ef61a8d68df88b49367f9afc MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. Aaron Greenlee
  3. http://aarongreenlee.com/
  4. This work is licensed under a Creative Commons Attribution-Share-Alike 3.0
  5. Unported License.
  6. // Original Info -----------------------------------------------------------
  7. Author : Aaron Greenlee
  8. Created : 12/19/2010 9:47:00 AM
  9. @fileoverview Example of a Backbone.js Controller.
  10. @suppress {deprecated}
  11. // Modifications :----------------------------------------------------------
  12. */
  13. // ---------------------------------------------------------------------
  14. var DocumentController = Backbone.Controller.extend
  15. ({
  16. // -----------------------------------------------------------------
  17. // Controller Configuration
  18. // -----------------------------------------------------------------
  19. // A backbone.js controller listens to changes within the location
  20. // hash and executes the function (aka action) you associate to
  21. // the route.
  22. routes :
  23. {
  24. // When we first start the application, we will not have a hash
  25. // so we define our first route to be empty.
  26. "" : "welcome"
  27. // You could clear the hash; however, I prefer to use #home and
  28. // simply associate it with the same action as our default
  29. // route.
  30. ,"!/home" : "welcome"
  31. // Backbone uses a similar convention to the ColdBox
  32. // Framework or or Ruby on Rails. You can pass variables which
  33. // are represented by ':foo'. When the associated actions is
  34. // executed by backbone.js the 'message' will be passed as an
  35. // argument to the function.
  36. ,"!/extra/:message" : "extraAction"
  37. // For example, we want to show the detail view of a document.
  38. // So, we will make a route for that! Notice the "!" prefix.
  39. // I want to tell Googlebot to index this AJAX content. There
  40. // is more to it than just prefixing the hash with an
  41. // exclamation mark, but, you get the idea.
  42. ,"!/document/:documentNumber" : "detail"
  43. }
  44. // Constructor
  45. ,initialize: function(options)
  46. {
  47. // Create a reference to our Document Collection
  48. this.DocumentCollection = new DocumentCollection();
  49. // Create a reference to our DocumentAreaView
  50. this.DocumentArea = new DocumentAreaView
  51. ({
  52. // We will pass our DocumentCollection to our area view.
  53. // Our area view is responsible for 'many' models and can
  54. // listen to events such as 'sort', 'delete all' or perhaps
  55. // 'create new'.
  56. model: this.DocumentCollection
  57. });
  58. // Explicitly add a document to inform people who glance at
  59. // this backbone.js tutorial that the other models are randomly
  60. // generated and the text won't make much sene.
  61. var d = new Date();
  62. var instance =
  63. {
  64. id: 1
  65. ,summary: 'Example Document. Other documents will not make\
  66. sense as they are randomly generated from a list of words.'
  67. ,created: d.getMonth()
  68. + '/' + d.getDate()
  69. + '/' + d.getFullYear()
  70. ,modified: d.getMonth()
  71. + '/' + d.getDate()
  72. + '/' + d.getFullYear()
  73. ,description: 'This is an example document. The rest of the\
  74. the documents are randomly generated. The other documents\
  75. will not make much sense.'
  76. ,author: 'Aaron Greenlee'
  77. };
  78. this.DocumentCollection.add( new DocumentModel(instance) );
  79. // For demonstration purposes, I am now going to create some
  80. // example documents so we have something to see.
  81. var n = (Math.floor(Math.random() * 14) + 2);
  82. // Quickly ensure we have at least six
  83. n = (n < 6) ? 6 : n;
  84. // Make our documents
  85. for (var i = 2; i < n; i++)
  86. {
  87. this.DocumentCollection.addRandomDocument(i);
  88. }
  89. // Render our current application view. You may consider this:
  90. // * A Master View for the current activity.
  91. // * A Layout for the current activity.
  92. // * The area of the screen that holds all the individual
  93. // views and listens for 'global' or 'many-model' events.
  94. this.DocumentArea.render();
  95. return this;
  96. }
  97. // -----------------------------------------------------------------
  98. // Actions
  99. // -----------------------------------------------------------------
  100. // Our default home action.
  101. // @route: "", "home"
  102. ,welcome : function ()
  103. {
  104. return this;
  105. }
  106. // An example of an action that accepts URL variables.
  107. // @route: "extra/message";
  108. ,extraAction : function (message)
  109. {
  110. // I simply want to demonstrate the ability to do something with
  111. // URL variables. The view is not changing.
  112. alert(message);
  113. return this;
  114. }
  115. // @route document/:cid
  116. ,detail : function (documentNumber)
  117. {
  118. var DocumentModel = this.DocumentCollection.get(documentNumber);
  119. // Because we have random numbers, on a reload, this model may
  120. // no longer exist.
  121. if (typeof DocumentModel != 'object')
  122. {
  123. window.location.hash = 'home';
  124. }
  125. this.DocumentArea.showDocument(DocumentModel);
  126. }
  127. });