PageRenderTime 58ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/cdnsrc/js/view/DocumentDetailView.js

https://github.com/tauren/Learning-backbone.js
JavaScript | 81 lines | 34 code | 13 blank | 34 comment | 0 complexity | 5a8ec92b18717e39ecc75ed6da6e6daf 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:53 AM
  9. The detail view of a document for an example Backbone.js application.
  10. // Modifications :----------------------------------------------------------
  11. */
  12. var DocumentDetailView = Backbone.View.extend({
  13. // Constructor
  14. initialize: function ()
  15. {
  16. _.bindAll(this, "render");
  17. this.model.bind('change', this.render);
  18. this.model.addView(this);
  19. this.name = 'DocumentDetailView';
  20. return this;
  21. }
  22. // Bind events which can occur within our view to actions specific to
  23. // this instance of the view.
  24. ,events:
  25. {
  26. 'click .delete' : 'deleteDocument'
  27. }
  28. ,render: function ()
  29. {
  30. // Use the jQuery Template plug-in built by Microsoft to
  31. // render our model. The model will be provided to the template
  32. // as JSON using the toJSON method provided by BackboneJS.
  33. //
  34. // For this example, the templates exist within the initial HTML
  35. // rendered on the server; however, I plan to push these from
  36. // the CDN.
  37. $(this.el).html(
  38. $.tmpl('DetailTemplate',this.model.toJSON() )
  39. );
  40. // Return this to allow chaining.
  41. return this;
  42. }
  43. // Destroy the model
  44. ,deleteDocument: function()
  45. {
  46. // The model will now remove all of it's views.
  47. this.model.unloadViews();
  48. // Remove the model from the collection
  49. this.model.collection.remove(this.model);
  50. // Return this to allow chaining.
  51. return this;
  52. }
  53. ,unloadView : function ()
  54. {
  55. // We are not going to delete this specific view from the DOM.
  56. // We want to inform the user they deleted the model.
  57. $(this.el).html(
  58. $.tmpl('DeletedDocumentTemplate',{id: this.model.id} )
  59. );
  60. // Return this to allow chaining.
  61. return this;
  62. }
  63. });