PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/cdnsrc/js/view/DocumentAreaView.js

https://github.com/tauren/Learning-backbone.js
JavaScript | 109 lines | 53 code | 20 blank | 36 comment | 2 complexity | a21ea49ebe0f406f65594861762e61ce 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:53:28 AM
  9. Document Area View for a Backbone.js example application.
  10. // Modifications :----------------------------------------------------------
  11. */
  12. var DocumentAreaView = Backbone.View.extend
  13. ({
  14. // Views always have a DOM element, even if it is invisible in the
  15. // page. This area view will be bound to an existing element, so, I
  16. // will use a jQuery selector to select the element. If I had not
  17. // done selected an existing element -- or defined a 'tagName',
  18. // backbone.js would have created a <div /> element that is simply
  19. // not yet associated with the document.
  20. el : $('#container')
  21. ,events : {
  22. 'click span.logCollection' : 'logCollection'
  23. }
  24. ,selectedLast: null
  25. ,selectedModel: null
  26. // Initialize our view.
  27. ,initialize: function () {
  28. //_.bindAll(this, 'addDocument', 'addAll', 'render');
  29. _.bindAll(this, 'addDocument');
  30. this.model.bind('refresh', this.render);
  31. //this.model.bind('all', this.render);
  32. this.layout = DocumentAreaTemplate;
  33. // Return this to allow chaining.
  34. return this;
  35. }
  36. ,render : function ()
  37. {
  38. // Use jQuery to select this view's element.
  39. $(this.el)
  40. .empty()
  41. .html(this.layout);
  42. // Render our Collection
  43. this.model.each(this.addDocument)
  44. // Return this to allow chaining.
  45. return this;
  46. }
  47. ,addDocument : function (Doc)
  48. {
  49. var ModelView = new DocumentListView
  50. ({
  51. // Bind the new list-item view to our new model instance.
  52. model : Doc
  53. // Optionally, you may also provide a id for the new HTML
  54. // element by passing it to our constructor.
  55. ,id : 'document_' + Doc.id
  56. ,parent : this
  57. });
  58. this.$('#documentListing ul').append( ModelView.render().el );
  59. // Return this to allow chaining.
  60. return this;
  61. }
  62. ,showDocument : function (Doc)
  63. {
  64. this.selectedModel = Doc;
  65. if(this.selectedLast) {
  66. this.selectedLast.change();
  67. }
  68. if(!this.selectedModel.hasChanged()) {
  69. this.selectedModel.change();
  70. }
  71. var ModelView = new DocumentDetailView({model : Doc});
  72. $('#documentDetailContainer').empty().html(
  73. ModelView.render().el
  74. );
  75. // Return this to allow chaining.
  76. return this;
  77. }
  78. // A simple helper method useful while I was developing the delete
  79. // feature.
  80. ,logCollection : function ()
  81. {
  82. console.log(this.model.models.length);
  83. }
  84. });