ember.js - 为什么模型在 Controller 内部无法访问,而在 Handlebars 模板中可以访问?

标签 ember.js ember-data

我有一个患者对象的模型

 App.Router.map (function () {
    this.resource('patients');
    this.resource('patient', {path: ':patient_id'}, function(){
         this.resource('dashboard', function() {
             this.route('summary');

         });

    });
});




   App.PatientRoute = Ember.Route.extend({
          model: function(params) {
            return App.Patient.find(params.patient_id);
          },
          setupController: function(){
              console.log("Menu Items:" + App.PatientMenuItem.find() );
              this.controllerFor('patient').set('menuItems', App.PatientMenuItem.find());

          }
        });


App.DashboardSummaryRoute = Ember.Route.extend({
    setupController: function(){

        this.controllerFor('dashboard.summary').set('content', this.controllerFor('patient').get('model'));

        this.controllerFor('dashboard.summary').getPatient();
    }

});


App.DashboardSummaryController = Ember.ObjectController.extend({

  getPatient:function(){
      console.log(this.content.get_allergies);
  }
});

App.PatientController = Ember.ObjectController.extend({
    menuItems:[],    

});


<script type="text/x-handlebars" data-template-name="dashboard/summary">
Summary{{this.content.get_allergies}}
</script>

在上面,我无法在 DashboardSummaryController 中访问相同的 get_allergies,但我可以在 Handlebars 中访问它,任何人都可以帮助我,这是什么错误?

提前致谢

最佳答案

我不知道单独这样做是否可以解决问题,但是在访问属性时始终使用 get() 和 set() 方法。所以我建议在 getPatient() 方法中尝试一下:

App.DashboardSummaryController = Ember.ObjectController.extend({

  getPatient:function(){
      console.log(this.get("content.get_allergies"));
  }
});

为什么模板有效?您那里的 Handlebars 表达式会自动转换为调用,我已建议您的 Controller 方法。

关于ember.js - 为什么模型在 Controller 内部无法访问,而在 Handlebars 模板中可以访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15199600/

相关文章:

javascript - Ember 测试路由重定向

ember.js - 将负 isDirty 与禁用类连接

javascript - Ember Data 从存储中抓取已加载的对象

ruby-on-rails - Ember.js/Rails和关联: how to submit back to Rails a record and its associations?

ember.js - Ember {嵌入: 'always' } on model Vs Serializer

ember.js - EmberJS 2 中的路由更改通知组件

ember.js - 您可以使用两个HTML模板,例如Handlebars和Jinja

ember.js - Ember -- TypeError : arrangedContent. addArrayObserver 不是函数

javascript - Ember在ember-data 1.13.8中获取相关记录

javascript - Ember 2,性能困境,路由或 Controller 中的计算属性?