为什么在车把模板中可以访问控制器内部的模型?

时间:2013-03-04 10:38:49

标签: 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,但我可以在把手中访问它,任何人都可以帮我解决这个错误吗?

提前致谢

1 个答案:

答案 0 :(得分:3)

我不知道这是否能解决问题,但在访问属性时总是使用get()和set()方法。所以我建议你在你的getPatient()方法中尝试这个:

App.DashboardSummaryController = Ember.ObjectController.extend({

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

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