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

/doc/more_about_views.md

https://github.com/raganwald-deprecated/faux
Markdown | 199 lines | 151 code | 48 blank | 0 comment | 0 complexity | 7d80843205e2584c3de9dfe3cf092e4a MD5 | raw file
  1. More About Views
  2. ===
  3. The naiïve way to write a controller method in Faux looks like this:
  4. VestamentsView = Backbone.View.extend({ ... });
  5. magic_controller
  6. .method('vestaments', {
  7. route: '/vestaments/:colour',
  8. clazz: VestamentsView
  9. });
  10. If you like convention over configuration, you can also write:
  11. VestamentsView = Backbone.View.extend({ ... });
  12. magic_controller
  13. .method('vestaments', {
  14. route: '/vestaments/:colour'
  15. });
  16. If you don't specify a `clazz` but Faux can find a view class that is named after your method, Faux will use it. That works for methods that look like they're singular or plural:
  17. ThaumaturgyView = Backbone.View.extend({ ... });
  18. magic_controller
  19. .method('thaumaturgy', { ... ); // infers clazz: ThaumaturgyView
  20. There's another special case for method names that look like plurals:
  21. SpellCollectionView = Backbone.View.extend({ ... });
  22. magic_controller
  23. .method('spells', { ... }); // infers clazz: SpellCollectionView if it can't find SpellsView first
  24. If you don't want a view class, you can always insist:
  25. magic_controller
  26. .method('something', {
  27. clazz: false
  28. });
  29. We're not big fans of global namespace clutter. If you feel the same way, start like this:
  30. window.ca || (window.ca = {});
  31. window.ca.unspace || (window.ca.unspace = {});
  32. magic_controller = new Faux.Controller({
  33. element_selector: '.base',
  34. partial: 'hamljs',
  35. partial_suffix: '.haml',
  36. namespace: ca.unspace // <--- lookie here
  37. });
  38. And now you can write:
  39. ca.unspace.VestamentsView = Backbone.View.extend({ ... });
  40. magic_controller
  41. .method('vestaments', {
  42. route: '/vestaments/:colour'
  43. });
  44. Some folks are big fans of point-free syntax and anonymous functions. Faux digs your groove, too:
  45. magic_controller
  46. .method('vestaments', {
  47. route: '/vestaments/:colour',
  48. clazz: {
  49. // equivalent to Backbone.View.extend({ ... })
  50. }
  51. });
  52. **models**
  53. [Bacbone.js][b]'s views are great for managing interaction unobtrusively. Each view can have a model that serves as the source of the data displayed by its templates. The easiest way to initialize a view with data is to assign in to the `model` parameter:
  54. StaffView = Backbone.View.extend({
  55. // elided
  56. });
  57. magic_controller
  58. .method('staff', {
  59. gets: { model: '/staff/:id' }
  60. });
  61. This places the raw data for a staff into the view's `model` property, where it can be rendered by the template or accessed by view methods.
  62. Models in Backbone.js can also be managed with a [Model Class][mc] or [Collection Class][cc]. This is useful when you want to do things like automatically re-render a view when some data is updated:
  63. CrystalBall = Backbone.Model.extend({
  64. // elided
  65. });
  66. CrystalBallView = Backbone.View.extend({
  67. initialize: function () {
  68. var view = this;
  69. view.model.bind('change', function () {
  70. view.render();
  71. })
  72. }
  73. });
  74. magic_controller
  75. .method('crystal_ball', {
  76. gets: '/ball/:id'
  77. before_display: {
  78. model: function (params) { return new CrystalBall(params.ball); }
  79. }
  80. });
  81. Just as Faux is able to deduce the view class from the name of the controller method you declare, Faux is also able to deduce the name of a model class or collection class. The above declaration can just as easily be written:
  82. // "CrystalBall" or "CrystalBallModel" and "CrystalBallView" or "CrystalBallModelView" declared previously
  83. magic_controller
  84. .method('crystal_ball', {
  85. gets: { model: '/ball/:id' }
  86. });
  87. Faux creates a new instance of `CrystalBall` and assigns it to the instance of `CrystalBallView` automatically. Faux does the same thing with Backbone.js's [Collection Classes][cc] when you declare a method that looks like a plural:
  88. SpellCollection = Backbone.Collection.extend({
  89. // elided
  90. });
  91. SpellsView = Backbone.View.extend({
  92. // elided
  93. });
  94. magic_controller
  95. .method('spells', {
  96. gets: { model: '/spells' }
  97. });
  98. Sometimes, you want to pick your own model class:
  99. magic_controller
  100. .method('familiars', {
  101. // elided
  102. model_clazz: SomeOtherModel
  103. });
  104. And sometimes, you don't want to use a model class even if you've declared one with the proper name. No problem:
  105. FamiliarModel = Backbone.Model.extend({
  106. // elided
  107. });
  108. magic_controller
  109. .method('familiars', {
  110. // elided
  111. model_clazz: false
  112. });
  113. With Faux, you're always in control. There's no mystery, Faux is simply wiring things together that you create.
  114. **More Reading**
  115. * [Faux][readme]
  116. * [Writing an Application with Faux][w]
  117. * [More About Views][v]
  118. * [Configuration Options][c]
  119. * [Functions][f]
  120. *Faux and its documentation is still a work in progress: Future additions to this document may or may not include discussions about handling error codes, directly invoking methods, unobtrusive handlers, and some of the other macros such as `title`, `infers`, `redirects_to`, and `location`.*
  121. [aanand]: http://github.com/aanand/
  122. [api]: http://www.joelonsoftware.com/articles/APIWar.html "How Microsoft Lost the API War"
  123. [b]: http://documentcloud.github.com/backbone/
  124. [cloud]: http://getcloudkit.com/
  125. [core]: http://www.ridecore.ca "CORE BMX and Boards"
  126. [couch]: http://couchdb.apache.org/
  127. [cps]: http://en.wikipedia.org/wiki/Continuation-passing_style "Continuation-passing style - Wikipedia, the free encyclopedia"
  128. [c]: /raganwald/faux/tree/master/doc/config.md#readme
  129. [cc]: http://documentcloud.github.com/backbone/#Collection
  130. [functional]: http://osteele.com/sources/javascript/functional/
  131. [f]: /raganwald/faux/tree/master/doc/functions.md#readme
  132. [haml]: http://haml-lang.com/ "#haml"
  133. [jamie]: http://github.com/jamiebikies
  134. [k]: https://github.com/raganwald/JQuery-Combinators
  135. [mvp]: http://github.com/raganwald/homoiconic/blob/master/2010/10/vc_without_m.md#readme "MVC, PVC and (¬M)VC"
  136. [m]: /raganwald/faux/tree/master/doc/methods.md#readme
  137. [mc]: http://documentcloud.github.com/backbone/#Model
  138. [prg]: http://en.wikipedia.org/wiki/Post/Redirect/Get
  139. [raganwald]: http://github.com/raganwald
  140. [read]: http://weblog.raganwald.com/2007/04/writing-programs-for-people-to-read.html "Writing programs for people to read"
  141. [readme]: /raganwald/faux/tree/master/docREADME.md#readme
  142. [sinatra]: http://www.sinatrarb.com/
  143. [spa]: http://en.wikipedia.org/wiki/Single_page_application "Single Page Application"
  144. [spi]: http://itsnat.sourceforge.net/php/spim/spi_manifesto_en.php "The Single Page Interface Manifesto"
  145. [sprout]: http://www.sproutcore.com/
  146. [s]: http://github.com/quirkey/sammy "sammy_js"
  147. [todo]: http://documentcloud.github.com/backbone/examples/todos/index.html
  148. [t]: https://github.com/raganwald/homoiconic/blob/master/2008-10-30/thrush.markdown
  149. [v]: /raganwald/faux/tree/master/doc/more_about_views.md#readme
  150. [w]: /raganwald/faux/tree/master/doc/writing.md#readme
  151. [wicmajsp]: http://raganwald.posterous.com/why-i-call-myself-a-javascript-programmer "Why I Call Myself a Javascript Programmer"