2013-07-08 22 views
1

我在海上,對於任何幫助,感謝Ember的最新版本。使用todo示例,我試圖在以下修改中包含路由和DS模型http://jsfiddle.net/5HMzu/3/ember.js模型數據未被collectionView輸出

當我在車把模板中使用#each控制器時,輸出如預期;用CollectionView代替它,並試圖將其與數據源綁定,沒有任何東西正在輸出。

所插入的代碼:

App.PersonCollectionView = Ember.CollectionView.extend({ 
itemViewClass: "App.ListOfPeopleTemplateView", 
emptyView: Ember.View.extend({ 
    template: Ember.Handlebars.compile("Oh dear, the collectionView is empty") 
}), 
contentBinding: "App.listOfpeopleTemplateController" 

});

結果: 「哦,親愛的,則是的CollectionView空」

模板

<body> 
<script type="text/x-handlebars"> 
    <!-- an attempt to show the data template 'listOfPeopleTemplate' using a CollectionView --> 
    {{view App.PersonCollectionView}}  
</script>  
<script type="text/x-handlebars" data-template-name="listOfPeopleTemplate">  
    {{test}} 
    <!-- This was working before I removed the #each and tried to use a CollectionView --> 
    <!-- {{#each controller}} --> 
    <fieldset>    
     <label {{bindAttr for="view.tbFullName.elementId"}}>Full Name</label> 
     {{view App.DetailTextField viewName="tbFullName" placeholder="Full Name" valueBinding="fullName" readonly="readonly"}}     
     <br/> 
     <label {{bindAttr for="view.tbFirstName.elementId"}} class="RequiredLabel">First Name</label> 
     {{view App.DetailTextField viewName="tbFirstName" valueBinding="firstName"}} 
     <br/>     
     <label {{bindAttr for="view.tbSurname.elementId"}} class="RequiredLabel">Surname</label> 
     {{view App.DetailTextField viewName="tbSurname" placeholder="surname" valueBinding="surname"}} 
     <br/>     
    </fieldset> 
    <!-- {{/each}} --> 
</script>  

JS

//the application 
App = Ember.Application.create(); 
//the models 
App.PersonModel=DS.Model.extend({ 
    personID: DS.attr('number'), 
    firstName: DS.attr('string'), 
    surname: DS.attr('string'), 
    fullName: Ember.computed(function() { 
     return this.get('firstName') + ' ' + this.get('surname'); 
     // Tell Ember that this computed property depends on firstName 
     // and lastName 
    }).property('firstName', 'surname') 
}); 

App.Store=DS.Store.extend({ 
    revision: 12, 
    adapter: 'DS.FixtureAdapter' 
}); 
App.PersonModel.FIXTURES = [{ 
    id: 1, 
    "personID": "1", 
    "firstName": "small", 
    "surname": "Hick" 
}, { 
    id: 2, 
    "personID": "2", 
    "firstName": "Katherine", 
    "surname": "Hosh" 
}]; 

App.Router.map(function() { 
    this.resource('listOfPeopleTemplate', { path: '/' }); 
}); 
//Defining a resource 'listOfPeopleTemplate' will make ember automatically generate a ListOfPeopleTemplateRoute, a ListOfPeopleTemplateController and a ListOfPeopleTemplateView 
//You can override this default setup by defining them yourself. This I have done below. 

App.ListOfPeopleTemplateRoute = Ember.Route.extend({ 
    model: function() { 
     return App.PersonModel.find(); 
    } 
}); 
App.ListOfPeopleTemplateController=Ember.ArrayController.extend({ 
    test:"does this work test" 
}); 

App.ListOfPeopleTemplateView=Ember.View.extend({templateName:"listOfPeopleTemplate"}); 

App.DetailTextField = Em.TextField.extend({ 
    attributeBindings: ['required', 'readonly', 'name'] 
}); 
//New code added to try and use the handlebar template with an ember.collectionView 
App.listOfpeopleTemplateController=App.ListOfPeopleTemplateController.create(); 
App.PersonCollectionView = Ember.CollectionView.extend({ 
    itemViewClass: "App.ListOfPeopleTemplateView", 
    emptyView: Ember.View.extend({ 
     template: Ember.Handlebars.compile("Oh dear, the collectionView is empty") 
    }), 
    contentBinding: "App.listOfpeopleTemplateController" 
}); 

回答

1

有幾件事情不對勁這裏。

  • 你永遠不會直接實例化控制器,Ember會爲你做這件事。避免SomeController.create
  • 當你切換到CollectionView你需要給它什麼屬性來綁定,在這種情況下,內容。
  • 您直接在PersonCollectionView上提供了此信息,但該綁定假定爲控制器的靜態路徑。這不適用於Ember的容器。您可以提供controllerBinding,但假定該控制器的路線和模型具有該模型。在這種情況下,您仍然在application路線上。通常,您只需在模板中設置content即可。

我相信這些東西大部分是由於參考舊版Ember版本的教程/代碼。從那以後,Ember發生了重大變化,不幸的是,網上發現的很多示例代碼現在已過時。

我定了一點東西here,通過把裏面的東西ApplicationRoute,加入contentBinding

一些建議我可以做,

  • 嘗試通過Ember getting started guide開始去完成。
  • 通過樣本示例,如this,並嘗試從頭開始重建它們。
  • 避免CollectionView,並使用#each{{outlet}}。 CollectionView僅用於特殊情況,例如在列表中呈現10000個以上的項目等。#each在內部實施時具有更智能的CollectionView
+0

謝謝。顯然有更多的學習框架。使用最新的庫來查找我的應用程序非常令人沮喪。採取你的帶頭,我會回到#each,但我認爲這就是爲什麼我不能綁定標籤對於字段:燼只是鏈接到最後一個記錄,而不是每個記錄的輸入字段(以便當用戶點擊在標籤上,正確的輸入欄被突出顯示)。 – user2192333