【问题标题】:EmberJS - object proxying is deprecated - accessing property of a controller in templateEmberJS - 不推荐使用对象代理 - 访问模板中控制器的属性
【发布时间】:2015-09-06 06:47:10
【问题描述】:

我试图了解某些特殊性。

在一个控制器中设置xxx 属性并迭代#each 有效,而与yyy #each 看似相同的操作却不...

我包括代码的亮点和可运行的代码sn-p:


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 的修复方法是什么?

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

【问题讨论】:

  • 您的 sn-p 不会触发对象代理弃用。此外,您没有包含 Color 模型的声明。
  • 谢谢@Daniel - 我已经包含了Color 模型(这有关系吗?)并包含一个显示弃用警告的屏幕截图。我的问题仍然有效 - controller.xxx 有效,而 controller.yyy 无效...

标签: javascript ember.js handlebars.js htmlbars


【解决方案1】:

好的,正如我所料 - 问题在于命名约定和过去的遗物(ObjectController)。声明 ColorController 为模型创建控制器,而不是路由。您需要在此处使用控制器进行路由,因此将 ColorController 更改为 ColorShowController 可以解决问题和值渲染。已弃用。

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>

【讨论】:

  • 致任何改变我解释的编辑——我确定我写的东西,我不是说color 路线,而是color 作为模型。请阅读更多关于 ObjectController 及其在实践中的工作原理的信息。
  • 如果这只是color 路由的控制器,那么为什么它会在color.show 路由中引发来自控制器的弃用? ; )
  • 实际发生的情况是 ColorController 为 ColorRoute 设置了控制器,因此留下 Ember 为 ColorShowRoute 生成一个控制器。 Ember 将根据模型挂钩返回的内容生成 ObjectController 或 ArrayController,这就是弃用警告的来源。附言ember-cli 0.2.7 包含一个禁用代理控制器生成的插件。
  • @locks 好的,我认为你是对的。谢谢解释!
  • 谢谢@Daniel,谢谢大家。我在工作项目中使用 CLI,但为了独立测试用例,我必须创建非 CLI 模板 - discuss.emberjs.com/t/… - 当我这样做时,我遇到了这种特殊性(一旦你知道这似乎很明显它)。
猜你喜欢
  • 1970-01-01
  • 2015-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-11
  • 2015-10-06
  • 1970-01-01
  • 2016-06-23
相关资源
最近更新 更多