2012-07-03 4 views
3

나는 바보 같은 짓을하고 있지만 내 백본 마리오 넷 응용 프로그램은 나에게 의미가없는 템플릿 오류를 제공하고 있음을 알고 있습니다. 가져 오기 이벤트가 발생하기 전에 단일 항목을 렌더링하는 것으로 보입니다. 내 태그 템플릿에서 콧수염을 제거하면 백본 마리오네트 가져 오기가 완료되기 전에 표시

_.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 페이지는 1 표시

<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(); 
     }); 

입니다 : "태그 이름"그것이 바로 수를 보여줍니다 완료된 후 때 가져옵니다. 내가 얻을에 다시 콧수염을 넣으면

나는 내가 뒤로 내 패턴 중 하나가 느낌 "태그 이름이 정의되지 않았습니다." 나는 그것을보기에는 너무 가깝다.

감사 마가 복음

답변

3

문제는 당신의 초기화

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

에서이 라인, 백본 컬렉션에 빈 모델을 생성합니다. 이 문제를 해결하려면, 단지 객체 리터럴 제거 :

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를 느낀다 그러나 그것은 나에게 작동하는 app를 준다. 당신이 Backbone.Collection 생성자에 리터럴 빈 개체를 전달할 때

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

나는 똑같은 짓을했다. 더 나은 해결책을 찾고 싶다. –

관련 문제