【问题标题】:ember.js model data is not being output by the collectionViewcollectionView 未输出 ember.js 模型数据
【发布时间】:2013-07-08 16:59:33
【问题描述】:

我在海上,对于最新版本的 Ember 的任何帮助,我将不胜感激。使用todo 示例,我尝试在以下改编的http://jsfiddle.net/5HMzu/3/ 中包含路由和DS 模型

当我在车把模板中使用#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"   
 });

【问题讨论】:

    标签: ember.js handlebars.js ember-data


    【解决方案1】:

    这里有一些问题。

    • 您永远不会直接实例化控制器,Ember 会为您执行此操作。避免SomeController.create
    • 当您切换到CollectionView 时,您需要为其提供要绑定的属性,在本例中为内容。
    • 您直接在PersonCollectionView 上提供了这个,但绑定假定一个控制器的静态路径。这不再适用于 Ember 的容器。您可以提供controllerBinding,但这假定该控制器的路由和模型具有模型。在这种情况下,您仍然在 application 路线上。通常您只需在模板中设置content

    我相信其中大部分是由于参考了旧版 Ember 的教程/代码。自那以后,Ember 发生了显着变化,不幸的是,网上找到的许多示例代码现在已经过时了。

    我对@9​​87654321@ 进行了一些修正,将东西放入ApplicationRoute,添加contentBinding,等等。

    我可以提出一些建议,

    • 尝试通过 Ember getting started guide 开始到结束。
    • 浏览this 之类的示例,并尝试从头开始重建它们。
    • 避免使用CollectionView,并使用#each{{outlet}}。 CollectionView 仅在特殊情况下需要,例如在列表中呈现 10,000 多个项目等。#each 在内部实现了更智能的CollectionView

    【讨论】:

    • 谢谢。显然还有更多的概念框架需要学习。使用最新的库发现我的应用程序损坏非常令人沮丧。带头,我将回到#each,但我认为这就是为什么我无法将标签 For 绑定到字段的原因:ember 只是将其链接到最后一条记录,而不是每条记录的输入字段(这样当用户单击在标签上,正确的输入字段突出显示)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    相关资源
    最近更新 更多