CompositeView with Collection

时间:2013-09-02 22:00:50

标签: backbone.js collections marionette

*更新:已解决,请滚动下来*

使用下面的列表进行渲染,但不会渲染任何行。这是因为我没有在复合视图中定义集合。

如果我在list.js中输入:collection:new Users,那么我收到此错误:

Uncaught TypeError: Object function () {
        return parent.apply(this, arguments);
      } has no method 'on' 

在backbone.js中:第203行,即:

// An inversion-of-control version of `on`. Tell *this* object to listen to
    // an event in another object ... keeping track of what it's listening to.
    listenTo: function (obj, name, callback) {
      var listeners = this._listeners || (this._listeners = {});
      var id = obj._listenerId || (obj._listenerId = _.uniqueId('l'));
      listeners[id] = obj;
      obj.on(name, typeof name === 'object' ? this : callback, this);
      return this;
    },

视图

row.js

define([
  'marionette',
  'text!app/views/templates/user/row.html'
],
  function (Marionette, Template) {
    "use strict"

    return Marionette.ItemView.extend({
      template: Template,
      tagName: 'tr'
    })

  })

list.js

define([
  'marionette',
  'text!app/views/templates/user/list.html',
  'app/collections/users',
  'app/views/user/row'
],
  function (Marionette, Template, Users, User) {
    "use strict"


    return Backbone.Marionette.CompositeView.extend({


      template: Template,
      itemView: User,
      itemViewContainer: "tbody",


    })
  })

这已通过以下方式解决:

用户/ list.js

define([
  'marionette',
  'text!app/views/templates/user/list.html',
  'app/collections/users',
  'app/views/user/row'
],
  function (Marionette, Template, Users, User) {
    "use strict"

    return Backbone.Marionette.CompositeView.extend({
      template: Template,
      itemView: User,
      itemViewContainer: "tbody",
      initialize: function() {
        this.collection = new Users()
        this.collection.fetch()
      }
    })
  })

用户/ row.js

define([
  'marionette',
  'text!app/views/templates/user/row.html'
],
  function (Marionette, Template) {
    "use strict"

    return Backbone.Marionette.ItemView.extend({
      template: Template,
      tagName: "tr"
    })
  })

1 个答案:

答案 0 :(得分:0)

你确定你是

吗?
  1. 将集合实例传递给复合视图?即new Users()
  2. 获取要在集合上显示fetch()的数据?