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

/src/views/listview.js

https://bitbucket.org/yashdoshi89/rjs-compile-test
JavaScript | 80 lines | 46 code | 13 blank | 21 comment | 4 complexity | 6e74807fc418187fe42ccf314eaaf33d MD5 | raw file
  1. define([
  2. 'underscore',
  3. 'backbone',
  4. 'views/singleview'
  5. ], function (
  6. util,
  7. Backbone,
  8. SingleView
  9. ) {
  10. var slice = Array.prototype.slice;
  11. // An ordered set of sub-views.
  12. // Conceptually corresponds to an array.
  13. // Usage:
  14. //
  15. // var view = new ListView({
  16. // view: MyViewCtor
  17. // });
  18. // view.collection.add(new Backbone.Model());
  19. var ListView = Backbone.View.extend({
  20. initialize: function (options) {
  21. options = options || {};
  22. this.collection = options.collection || new Backbone.Collection();
  23. // Set target element for subviews. Use scoped selector if element
  24. // isn't `this.el`.
  25. this.target = options.target ? this.$(options.target) : this.el;
  26. // Set subview constructor.
  27. this.view = options.view || SingleView;
  28. if (options.bridge) this.bridge = options.bridge;
  29. this.collection
  30. .bind('add', this.addView, this)
  31. .bind('reset', this.resetViews, this);
  32. this.resetViews(this.collection);
  33. },
  34. // General factory for subviews.
  35. createView: function () {
  36. // Create an options object by getting all of the objects passed in.
  37. // This allows for multiple options objects to be curried onto the
  38. // function -- useful for specializing the way subviews are created.
  39. var options = util.extend.apply(this, arguments),
  40. // Pick a view.
  41. View = this.filterView.call(options, this.view),
  42. view = new View(options);
  43. // Fire an event, allow others to modify view.
  44. this.trigger('add', this, view);
  45. // Append a view to the DOM. It needs to know how to add and remove
  46. // itself.
  47. $(this.target).append(view.render().el);
  48. return this;
  49. },
  50. filterView: function (View) {
  51. return View;
  52. },
  53. // Specialized factory for use with binding to model events.
  54. addView: function (model) {
  55. this.createView({ model: model, bridge: this.bridge });
  56. },
  57. // Specialized factory for use with binding to collection events.
  58. resetViews: function (collection) {
  59. $(this.target).html('');
  60. collection.each(this.addView, this);
  61. },
  62. render: function () {
  63. return this;
  64. }
  65. });
  66. return ListView;
  67. });