首页 文章

EmberJS - 不推荐使用对象代理 - 访问模板中控制器的属性

提问于
浏览
1

我试图理解某些特殊性 .

设置 xxx 属性并在一个控制器中迭代 #each ,但看起来与 yyy #each 相同的操作不...

我包括代码和可运行代码片段的亮点:


App.IndexController = Ember.Controller.extend({
  xxx : [{name:"a"}, {name:"b"}], // this works just fine
});  

{{#each item in xxx}}
  <li>{{item.name}}</li>
{{/each}}

App.ColorController = Ember.Controller.extend({
  yyy : [{name:"c"}, {name:"d"}], // this triggers deprecation
  // You attempted to access `yyy` from ...
  // But object proxying is deprecated. Please use `model.yyy` instead
});

{{#each item in yyy}}
  <li>{{item.name}}</li>
{{/each}}
App = Ember.Application.create();

    App.Color = DS.Model.extend({
      name: DS.attr('string')
    });
    
    App.Router.map(function() {
      this.resource('color', function(){
        this.route('show', { path: ':color_id' });
      });
    });

    App.IndexRoute = Ember.Route.extend({
      
      model: function() {
        return [
                { id: 1, name: "Red" },
                { id: 2, name: "Blue" },
               ];
      }
    });   

    App.IndexController = Ember.Controller.extend({
      xxx : [{name:"a"}, {name:"b"}], // this works just fine
    });     

    App.ColorController = Ember.Controller.extend({
      init : function() {
        this._super();
        console.info("Just to double check, this controller gets initialised");
      },
      yyy : [{name:"c"}, {name:"d"}], // this triggers deprecation
      // You attempted to access `yyy` from ...
      // But object proxying is deprecated. Please use `model.yyy` instead
    });
<script type="text/x-handlebars">
    <h2>Ember Starter Kit</h2>
    {{outlet}}
  </script>
 
  <script type="text/x-handlebars" id="index">
    <h3>Index</h3>
    <ul>
      {{#each color in model}}
        <li>{{#link-to "color.show" color}} {{color.name}} {{/link-to}}</li>
      {{/each}}
    </ul>
    <ul>
      {{#each item in xxx}}
        <li>{{item.name}}</li>
      {{/each}}
    </ul>
   </script>

  <script type="text/x-handlebars" id="color/show">
    <h3>color/show</h3>
    <h4>{{ model.name }}</h4>
    <ul>
      {{#each item in yyy}}
        <li>{{item.name}}</li>
      {{/each}}
    </ul>
    {{#link-to "application"}}Go back to the list{{/link-to}}
  </script>
 
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
  <script src="http://builds.emberjs.com/tags/v1.13.2/ember.debug.js"></script>
  <script src="http://builds.emberjs.com/tags/v1.13.2/ember-template-compiler.js"></script>
  <script src="http://builds.emberjs.com/tags/v1.13.2/ember-data.js"></script>

我想了解更多:

  • 为什么它在一个案例中起作用而在另一个案例中不起作用?

  • 什么是Ember固定方式?

编辑:更新的代码段包括颜色模型 . 要触发弃用警告,请单击其中一种颜色(红色,蓝色)...这是我运行代码段时发生的情况:

enter image description here

1 回答

  • 2

    好吧,正如我所料 - 问题在于命名约定和过去的遗物( ObjectController ) . 声明 ColorController 为模型创建控制器,而不是路径 . 你需要这里的路由控制器,所以改变 ColorControllerColorShowController 解决问题和值渲染 . 贬值已经消失 .

    App = Ember.Application.create();
    
        App.Color = DS.Model.extend({
          name: DS.attr('string')
        });
        
        App.Router.map(function() {
          this.resource('color', function(){
            this.route('show', { path: ':color_id' });
          });
        });
    
        App.IndexRoute = Ember.Route.extend({
          
          model: function() {
            return [
                    { id: 1, name: "Red" },
                    { id: 2, name: "Blue" },
                   ];
          }
        });   
    
        App.IndexController = Ember.Controller.extend({
          xxx : [{name:"a"}, {name:"b"}], // this works just fine
        });     
    
        App.ColorShowController = Ember.Controller.extend({
          init : function() {
            this._super();
            console.info("Just to double check, this controller gets initialised");
          },
          yyy : [{name:"c"}, {name:"d"}], // this triggers deprecation
          // You attempted to access `yyy` from ...
          // But object proxying is deprecated. Please use `model.yyy` instead
        });
    
    <script type="text/x-handlebars">
        <h2>Ember Starter Kit</h2>
        {{outlet}}
      </script>
     
      <script type="text/x-handlebars" id="index">
        <h3>Index</h3>
        <ul>
          {{#each color in model}}
            <li>{{#link-to "color.show" color}} {{color.name}} {{/link-to}}</li>
          {{/each}}
        </ul>
        <ul>
          {{#each item in xxx}}
            <li>{{item.name}}</li>
          {{/each}}
        </ul>
       </script>
    
      <script type="text/x-handlebars" id="color/show">
        <h3>color/show</h3>
        <h4>{{ model.name }}</h4>
        <ul>
          {{#each item in yyy}}
            <li>{{item.name}}</li>
          {{/each}}
        </ul>
        {{#link-to "application"}}Go back to the list{{/link-to}}
      </script>
     
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
      <script src="http://builds.emberjs.com/tags/v1.13.2/ember.debug.js"></script>
      <script src="http://builds.emberjs.com/tags/v1.13.2/ember-template-compiler.js"></script>
      <script src="http://builds.emberjs.com/tags/v1.13.2/ember-data.js"></script>
    

相关问题