2012-07-03 16 views
3

我知道我在做一些愚蠢的事情,但我的骨幹木偶應用程序給我的模板錯誤沒有任何意義。它似乎是在提取事件發生之前呈現單個項目。Backbone Marionette在抓取完成之前顯示

_.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:「變量名」,那麼當抓取完成它顯示了正確的號碼。

如果我把小鬍子早在我得到「標記名未定義」

我覺得我有倒退的我的模式之一。我太近了,看不到它。

感謝 馬克

回答

3

的問題是這條線在你的初始化

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

當你傳遞一個空對象文本中的Backbone.Collection構造,骨幹創建集合在一個空的模型。爲了解決這個問題,只是刪除對象的文字:

var tagCollection = new MyApp.TagCollection()

,它不會有空項目中它了。

+0

如果你想創建一個帶有ID的模型,你如何解決這個問題? –

+0

我不明白問題 –

+0

我在這裏創建了一個單獨的問題:http://stackoverflow.com/questions/13567305/how-to-prevent-backbone-marionette-from-rendering-a-view-if-它的模型,處理不當,是 –

0

嘗試:

tagCollection.fetch({ wait: true }); 
+0

這並沒有解決問題。 – MarkKGreenway

0

修改了模型有任何東西的默認值,我想模板。感覺Klugy,但它給了我一個可行的應用程序。

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

我已經做了同樣的...很想看到更好的解決方案,雖然 –

相關問題