PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/recipe-app-v2/src/infrastructure/backbone/recipe-app-view.js

https://github.com/ifandelse/devlink2012-Introduction-to-Backbone.js
JavaScript | 63 lines | 47 code | 7 blank | 9 comment | 2 complexity | 47bdcb5820359f7fabd7ffe1e14f2f03 MD5 | raw file
Possible License(s): MIT
  1. define([
  2. 'backbone',
  3. 'bus'
  4. ], function(Backbone, bus){
  5. return Backbone.View.extend({
  6. // Reduces method signature when switching UIs.
  7. // ex: this.vm.showUI('dashboardUI', {});
  8. vm : {
  9. // Shows another UI, allowing optional context args
  10. showUI : function (uiName, options) {
  11. bus.ui.publish({
  12. topic : "ui.show",
  13. data : {
  14. name : uiName,
  15. options : options
  16. }
  17. });
  18. }
  19. },
  20. // Similar idea to what backbone.marionette does, but tweaked.
  21. // This provides base "toJSON()" behavior for any view to call
  22. // for default-approach binding of model data to template, etc.
  23. dataToJSON: function() {
  24. var data;
  25. if (this.model) {
  26. data = this.model.toJSON();
  27. }
  28. if (this.collection) {
  29. data = {items: this.collection.toJSON()};
  30. }
  31. return data;
  32. },
  33. render: function(context) {
  34. this.trigger('preRender', context);
  35. this.$el.html(this.template(this.dataToJSON()));
  36. this.trigger('rendered', context);
  37. return this;
  38. },
  39. // Default "show" functionality.
  40. show: function (context) {
  41. this.$el.show();
  42. this.trigger("shown", context);
  43. },
  44. // Default "hide" functionality.
  45. hide: function (context) {
  46. this.$el.hide();
  47. this.trigger("hidden", context);
  48. },
  49. // Default "remove" functionality.
  50. remove: function() {
  51. this.undelegateEvents();
  52. this.$el.remove();
  53. return this;
  54. }
  55. })
  56. });