Backbone Marionette在获取完成之前显示

时间:2012-07-03 19:18:50

标签: backbone.js marionette

我知道我在做一些愚蠢的事情,但我的主干木偶应用程序给了我模板错误,这是没有意义的。它似乎是在fetch事件发生之前呈现单个项目。

_.templateSettings = {
    interpolate: /\{\{(.+?)\}\}/g
};


MyApp = new Backbone.Marionette.Application();
MyApp.addRegions({
    TagsRegion: "#tagsHolder"
});



MyApp.NoItemsView = Backbone.Marionette.ItemView.extend({
    template: "#show-no-items-message-template"
});


MyApp.Tag = Backbone.Model.extend({

});
MyApp.TagCollection = Backbone.Collection.extend({
    model: MyApp.Tag,
    url: '/API/Tag'
});
MyApp.TagItemView = Backbone.Marionette.ItemView.extend({
    template: "#tag-template",
    tagName: 'li'
});


MyApp.TagCollectionView = Backbone.Marionette.CollectionView.extend({
    itemView: MyApp.TagItemView,
    emptyView: MyApp.NoItemsView,
    tagName: 'ul'
});


MyApp.addInitializer(function(options){
    var tagCollection = new MyApp.TagCollection({
    });
    var tagCollectionView = new MyApp.TagCollectionView({
        collection: tagCollection
    });

    tagCollection.fetch();
    MyApp.TagsRegion.show(tagCollectionView);
});

我的html页面是

<div id="TagsDiv">
    <h1>Tags</h1>
    <div id="tagsHolder"></div>
</div>    
<script type="text/template" id="show-no-items-message-template">
    No items found.
</script>

<script type="text/template" id="tag-template">
    {{ TagName }}
</script>


    <script type="text/javascript" src="/Scripts/Views/Home/Upload.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {

            MyApp.start();
        });

如果我从标签模板中删除胡须,它会显示1:“TagName”,然后当提取完成时,它会显示正确的数字。

如果我把胡须放回去,我得到“TagName未定义”

我觉得我有一个向后的模式。我太近了,无法看到它。

由于 -Mark

3 个答案:

答案 0 :(得分:3)

问题在于初始化程序中的这一行

var tagCollection = new MyApp.TagCollection({
    });

当您将空对象文字传递给Backbone.Collection构造函数时,Backbone会在集合中创建一个空模型。要解决此问题,只需删除对象文字:

var tagCollection = new MyApp.TagCollection()

它将不再包含空白项目。

答案 1 :(得分:0)

尝试:

tagCollection.fetch({ wait: true });

答案 2 :(得分:0)

将模型修改为具有我想要模板的任何内容的默认值。感觉Klugy,但它给了我一个有效的应用程序。

MyApp.Tag = Backbone.Model.extend({
    defaults :
        {
            TagName : 'None'
        }
});