Ember API 笔记-Ember.Mixin

Emberjs API:http://emberjs.com/api/
定义于:packages/ember-metal/lib/mixin.js:306
所属模块:ember-metal

说明:Ember.Mixin类运行你创建mixins,mixins的属性可以被混合进其他类。使用方法见下面的例子,[注:Ember中很多类在继承时都使用了这个方式来模拟多继承]

App.Editable = Ember.Mixin.create({
  edit: function() {
    console.log('starting to edit');
    this.set('isEditing', true);
  },
  isEditing: false
});
// Mix mixins into classes by passing them as the first arguments to
// .extend or .create.
App.CommentView = Ember.View.extend(App.Editable, {
  template: Ember.Handlebars.compile('{{#if isEditing}}...{{else}}...{{/if}}')
});
commentView = App.CommentView.create();
commentView.edit(); // => outputs 'starting to edit'

注意,Mixins由Ember.Mixin.create创建,不是Ember.Mixin.extend。

继承的方法

-无

自有的方法

-apply(obj)

参数:
@obj--对象

返回:applied object

说明:作用未知,源代码如下

MixinPrototype.apply = function(obj) {
  return applyMixin(obj, [this], false);
};
-create(arguments)

参数:
@arguments--任意参数,非必须

返回:mixins实例。

说明:创建mixins实例。

-detect(obj)

参数:
@obj--对象

返回:布尔值

说明:用来检测传入的对象,具体作用未知,源代码如下

MixinPrototype.detect = function(obj) {
  if (!obj) { return false; }
  if (obj instanceof Mixin) { return _detect(obj, this, {}); }
  var mixins = Ember.meta(obj, false).mixins;
  if (mixins) {
    return !!mixins[guidFor(this)];
  }
  return false;
};
-reopen(arguments)

参数:
@arguments--任意参数,非必须

返回:this,对象本身

说明:当创建完类之后,可以在其它地方,通过该方法再次打开实例,继续添加新的属性。reopen是用来为实例添加属性和方法的。对应的,当你需要创建类的方法或为类本身添加属性时,可使用reopenClass。

继承的属性

-无

自有的属性

-无

你可能感兴趣的:(Ember API 笔记-Ember.Mixin)