首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CompositeView与收藏

CompositeView与收藏
EN

Stack Overflow用户
提问于 2013-09-02 22:00:49
回答 1查看 140关注 0票数 0

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

对于下面的内容,列表呈现,但不呈现行。这是因为我没有在复合视图中定义集合。

如果我确实在list.js中放置: collection: new User,那么我会得到以下错误:

代码语言:javascript
复制
Uncaught TypeError: Object function () {
        return parent.apply(this, arguments);
      } has no method 'on' 

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

代码语言:javascript
复制
// 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

代码语言:javascript
复制
define([
  'marionette',
  'text!app/views/templates/user/row.html'
],
  function (Marionette, Template) {
    "use strict"

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

  })

list.js

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
define([
  'marionette',
  'text!app/views/templates/user/row.html'
],
  function (Marionette, Template) {
    "use strict"

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

回答 1

Stack Overflow用户

发布于 2013-09-04 07:01:24

你确定你是

  1. 将集合实例传递给复合视图?即new Users()
  2. 获取要在集合上使用fetch()显示的数据?
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18581328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档