【问题标题】:Marionette.js collection is undefined in collection viewMarionette.js 集合在集合视图中未定义
【发布时间】:2015-11-10 22:16:13
【问题描述】:

我是 Marionette.js 的新手,在渲染集合视图时似乎遇到了一些问题。

我在尝试显示视图时收到以下控制台错误消息:Uncaught TypeError: Cannot read property 'toJSON' of undefined

似乎该集合没有绑定到子视图。集合视图的实例看起来不错,这意味着我看到了 childView 属性和带有获取模型的集合属性。关于如何构建集合视图的文档似乎非常简单,所以不确定我缺少什么。谢谢。

Child view:
var UserProfile = Marionette.ItemView.extend({
  template: '',
  initialize: function() {
    this.template = Marionette.TemplateCache.get("#userprofile");
  },
  render: function() {
    console.log(this.collection); //undefined
    var data = this.collection.toJSON(); //error message here
    this.$el.html(this.template({data:data}));
  } 
});

//Collection view:
var UserView = Marionette.CollectionView.extend({  
  childView: UserProfile
});

// Fetch collection and show:
var myQuery = new Parse.Query(app.Models.User);
myQuery.limit(200).containedIn(option, uiArray);

var Contacts = Parse.Collection.extend({
  model: app.models.User,
  query: myQuery
});

var contacts = new Contacts();
contacts.fetch().then(function(contacts) {
  var userview = new UserView({collection:contacts});
  app.regions.get("content").show(userview);
})

【问题讨论】:

    标签: javascript backbone.js marionette collectionview


    【解决方案1】:

    您在 ItemView 中询问“集合”。 ItemView 操作一个项目。所以写这样的东西是有意义的:

    render: function() {
        console.log(this.model); //undefined
        var data = this.model.toJSON(); //error message here
        this.$el.html(this.template({data:data}));
        }   
    });
    

    【讨论】:

    • 这似乎行得通。我想当我只想在应用程序的其他位置渲染单个模型时,这将允许我使用 ItemView?
    猜你喜欢
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-03
    相关资源
    最近更新 更多