木偶集合视图 - 不渲染集合

时间:2014-11-30 13:29:35

标签: jquery backbone.js marionette

我的集合视图无法正确呈现,而是抛出错误...

这是我的代码:

var ContactManager = new Marionette.Application();

        ContactManager.addRegions({
            content : "#main-region"
        })

        ContactManager.Contact = Backbone.Model.extend();

        ContactManager.Collection = Backbone.Collection.extend({
            model : ContactManager.Contact
        })

        ContactManager.ContactView = Marionette.ItemView.extend({
            template: "#contact-list-item"
        });


        ContactManager.on("start", function () {

            var contacts = new ContactManager.Collection([
                {
                    firstName: "Bob",
                    lastName: "Brigham",
                    phoneNumber: "555-"
                },
                {
                    firstName: "Alice",
                    lastName: "Arten",
                    phoneNumber: "555-0184"
                },
                {
                    firstName: "Charlie",
                    lastName: "Campbell",
                    phoneNumber: "555-0129"
                }
            ]);

            ContactManager.content.show(Marionette.CollectionView.extend({ //nothing renders here
                tagName: "ul",
                childView : ContactManager.ContactView,
                collection : contacts
            }));
        })
        ContactManager.start();

templates中的所有DOM都在{{}}},我收到错误消息:

Uncaught TypeError: undefined is not a function

有人帮我吗?

1 个答案:

答案 0 :(得分:3)

您只是遗漏了一件事,当您在内容区域调用show时,您需要传递集合视图的实例。

当Marionette尝试将事件绑定到您未实例化的视图时,它会抛出错误。

ContactManager.ContactCollectionView = Marionette.CollectionView.extend({ //nothing renders here
  tagName: "ul",
  childView : ContactManager.ContactView,
  collection : contacts
});

ContactManager.content.show(new ContactManager.ContactCollectionView());