DS.Model`inslations`属性在record的`init`中不可用?

时间:2015-01-04 14:10:41

标签: ember.js ember-data

官方documentation for DS.Model描述:

当我这样做时:

App.Parent = DS.Model.extend({
  children: DS.hasMany('child'),
  init: function() {
    this._super();

    this.eachRelationship(function(foo, bar, baz) {
      console.log('each relationship', foo, bar, baz);
    });
  }
});

...它打印出children关系。

然而,当我这样做时:

App.Parent = DS.Model.extend({
  children: DS.hasMany('child'),
  init: function() {
    this._super();

    console.log('realtionships', this.get('relationships'));
  }
});

...打印undefined

为什么呢?如何在记录初始化期间访问relationships属性,而不回复eachRelationship方法?

演示:http://emberjs.jsbin.com/vapama/9/edit?js,output

2 个答案:

答案 0 :(得分:2)

relationships是在不在实例上的类上定义的(属性名旁边的静态告诉我们这一点)。

var relationships = Em.get(App.Parent, 'relationships');

http://emberjs.jsbin.com/zebalupoba/1/edit

答案 1 :(得分:1)

有一个_relationships(带有下划线)属性可用Object

http://emberjs.jsbin.com/nudor/1/edit?html,js,console