骨干和牵线木偶,ItemView无法渲染,怎么样?

时间:2015-01-07 11:29:10

标签: javascript templates backbone.js views marionette

由于某种原因,ItemView不呈现。这是我的设置:

控制器

test: function () {
            require(['application', 'collections/collection-test', 'views/collection/view-test'], function (App, CollectionTest, ViewTest) {
                var collectionTest = new CollectionTest();

                App.regionTest.show(new ViewTest({
                    collection: collectionTest
                }));
            });
        },

集合视图

define(['backbone','views/item/itemview-test'],
function (Backbone, ItemviewTest) {
    'use strict';
    return Backbone.Marionette.CollectionView.extend({
        tagName: 'ul',
        initialize: function () {},
        itemView: ItemviewTest,
        ui: {},
        events: {},
        /* on render callback */
        onRender: function () {
            console.log(this.$el);
        }
    });
});

项目视图

define(['backbone','hbs!tmpl/item/itemview-test_tmpl'],
function (Backbone, ItemviewTestTmpl) {
    'use strict';
    return Backbone.Marionette.ItemView.extend({
        tagName: 'li',
        initialize: function (options) {},
        template: ItemviewTestTmpl,
        ui: {},
        events: {},
        onRender: function () {
            console.log(this.$el);
        },
    });
});

我可以看到<ul></ul>被添加到该地区,但不是li

我可以渲染li如果我用'views/collection/view-test' int eh控制器替换'views/item/itemview-test',但现在我不会得到ul

另外,如果我在集合中console.log(new ItemviewTest()),我可以看到带有ItemView标记的$el:'li'对象,但由于某种原因它不会被添加到html

任何想法?

1 个答案:

答案 0 :(得分:0)

为父ItemView集合中的每个模型呈现CollectionView。由于集合是空的,因此没有要渲染的模型。

尝试添加模型:

var collectionTest = new CollectionTest([{ name: 'Patrioticcow' }]);

App.regionTest.show(new ViewTest({
  collection: collectionTest
}));