Ember.js - 绑定到另一个控制器的模型

时间:2013-03-12 11:11:33

标签: ember.js

在我的项目中,我有以下三个模型:

  • 分会模特:代表一个体育俱乐部。它有一个clubMeta和多个customCourts
  • ClubMeta模型:与应用相关的俱乐部相关信息。
  • CustomCourt Model :属于club。仅当clubMeta.customCourtsEnabledtrue时,分会才应 customCourts

分会

var Club = DS.Model.extend({
    ... bunch of properties ...

    clubmeta: DS.belongsTo('App.Clubmeta'),
    customCourts: DS.hasMany('App.CustomCourt')
});

module.exports = Club;

Clubmeta

var Clubmeta = DS.Model.extend({
    ... bunch of properties ...
    customCourtsEnabled: DS.attr('boolean'),

    club: DS.belongsTo('App.Club')
});

module.exports = Clubmeta;

CustomCourt

var CustomCourt = DS.Model.extend({
    name: DS.attr('string'),
    glass: DS.attr('boolean'),
    indoor: DS.attr('boolean'),
    single: DS.attr('boolean'),

    club: DS.belongsTo('App.Club')
});

module.exports = CustomCourt;

我需要做的是一个模板,其中俱乐部(登录用户)只有在clubmeta.customCourtsEnabledtrue时才能添加 customCourts 。正如我在another SO question中被告知的那样,我应该使用ArrayController来处理CustomCourts

在此之前一切正常,问题出现是因为CustomCourtsController需要了解分会 clubmeta 。我试过这个,绑定路径有一些变化:

var ClubCourtsController = Ember.ArrayController.extend({
        needs: ['currentClub'],
        customCourtsEnabledBinding: Ember.Binding.oneWay("App.currentClubController.content.clubmeta.customCourtsEnabled"),
        ...
});

CurrentClubController

var CurrentClubController = Ember.ObjectController.extend({
    init: function() {
        this._super();
        console.log('Retrieving club ' + App.clubId);
        this.set('content', App.Club.find(App.clubId));
    }
});

module.exports = CurrentClubController;

但是ClubCourtsController.customCourtsEnabled总是返回undefined。这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:2)

  

ClubCourtsController.customCourtsEnabled始终返回undefined。这样做的正确方法是什么?

你走在正确的轨道上。对于初学者来说,进行绑定的正确方法是通过controllers属性:

var ClubCourtsController = Ember.ArrayController.extend({
    needs: ['currentClub'],
    customCourtsEnabledBinding:     Ember.Binding.oneWay("controllers.currentClub.clubmeta.customCourtsEnabled"),
});

除此之外,通常最好的做法是在路由的setupController挂钩而不是控制器init中将事物连接在一起。因此,即使您具有该绑定权限,也可能是导致customCourtsEnabled属性未定义的其他问题。我在jsbin上发布了一个工作示例:http://jsbin.com/ubovil/1/edit

App = Ember.Application.create({
  clubId: 1
});
App.Router.map(function() {
  this.route("customCourts", { path: "/" });
});
App.Club = Ember.Object.extend({});
App.Club.reopenClass({
  find: function(id) {
    return App.Club.create({
      id: id, 
      clubmeta: App.ClubMeta.create({
        customCourtsEnabled: true
      })
    });
  }
});
App.ClubMeta = Ember.Object.extend({});
App.CurrentClubController = Ember.ObjectController.extend({});

App.ApplicationController = Ember.Controller.extend({
  needs: ['currentClub']
});

App.ApplicationRoute = Ember.Route.extend({
  setupController: function(controller) {
    controller.set('controllers.currentClub.content', App.Club.find(App.clubId));
  }
});

App.CustomCourtsController = Ember.ArrayController.extend({
  needs: ['currentClub'],
  customCourtsEnabledBinding: Ember.Binding.oneWay("controllers.currentClub.clubmeta.customCourtsEnabled")
});

App.CustomCourtsRoute = Ember.Route.extend({
  setupController: function(controller) {
    controller.set('content', ['Court a', 'Court b', 'Court c']);
  }
});

答案 1 :(得分:1)

您需要通过controllers属性

访问控制器
ClubCourtsController = Ember.ArrayController.extend({
  needs: ['currentClub'],
  customCourtsEnabledBinding: Ember.Binding.oneWay("controllers.currentClub.clubmeta.customCourtsEnabled"),
  ...
});