简体   繁体   English

Emberjs itemController /控制器属性绑定

[英]Emberjs itemController / controller property binding

I've made a jsbin to illustrate my issue. 我做了一个jsbin来说明我的问题。

the binding seems KO with lastname property defined inside the itemController and the fullname value is not updated in my items loop. 绑定似乎在itemController中定义了具有lastname属性的KO,并且我的项循环中未更新fullname值。

What am I doing wrong ? 我究竟做错了什么 ?

Controller for item in list is different than one you edit property lastname for, so it will never get updated. 列表中项目的控制器与您为其编辑属性lastname控制器不同,因此它将永远不会更新。 Propery lastname has to be specified as Model's property (if using Ember Data you simply don't use DS.attr for it and it won't be persisted). 必须将属性lastname指定为Model的属性(如果使用Ember Data,则只需不使用DS.attr ,并且不会DS.attr该属性)。 If you use custom library for data persistence you have to manually remove lastname property. 如果使用自定义库进行数据持久化,则必须手动删除lastname属性。 You can use Ember Inspector extension to see that there are 5 controllers when you click on item. 单击项目时,您可以使用Ember Inspector扩展查看有5个控制器。 4 for each item in list and one is being generated when you click. 列表中的每个项目为4,单击时将生成一个。 You edit property lastname for this fifth controller. 您编辑此第五个控制器的属性lastname To solve this you can use: JavaScript: 要解决此问题,您可以使用:JavaScript:

App = Ember.Application.create();

App.Router.map(function() {
 this.resource('items', function() {
   this.resource('item', {path: '/:item_id'});
 });
});

App.Model = Ember.Object.extend({
   firstname: 'foo',
   lastname: 'bar',
   fullname: function() {
     return this.get('firstname') + ' ' + this.get('lastname');
   }.property('firstname', 'lastname')
});

App.ItemsRoute = Ember.Route.extend({
  model: function() {
    return [App.Model.create({id: 1}), App.Model.create({id: 2}), App.Model.create({id: 3}), App.Model.create({id: 4})];
  }
});

App.ItemRoute = Ember.Route.extend({
  model: function(params) {
    return this.modelFor('items').findBy('id', +params.item_id);
  }
});

Templates: 模板:

  <script type="text/x-handlebars">
    <h2>Welcome to Ember.js</h2>
    {{link-to "items" "items"}}
    {{outlet}}
  </script>

  <script type="text/x-handlebars" data-template-name="items">
    <ul>
    {{#each item in model}}
      <li>
        {{#link-to 'item' item.id}}
        {{item.fullname}} {{item.id}}
        {{/link-to}}
      </li>
    {{/each}}
    </ul>

    {{outlet}}
  </script>

   <script type="text/x-handlebars" data-template-name="item">
     {{input value=model.firstname}}
      {{input value=model.lastname}}
      {{model.fullname}}
  </script>

Please keep in mind that ArrayController and ObjectController aren't recommended to use, because they will be deprecated in future. 请记住,不建议使用ArrayControllerObjectController ,因为它们将来会被弃用。 Demo. 演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM