CoreObject.extend()

extend (mixins, arguments) publicstatic

Defined in packages/ember-runtime/lib/system/core_object.js:527

Creates a new subclass.

App.Person = Ember.Object.extend({
  say: function(thing) {
    alert(thing);
   }
});

This defines a new subclass of Ember.Object: App.Person. It contains one method: say().

You can also create a subclass from any existing class by calling its extend() method. For example, you might want to create a subclass of Ember's built-in Ember.View class:

App.PersonView = Ember.View.extend({
  tagName: 'li',
  classNameBindings: ['isAdministrator']
});

When defining a subclass, you can override methods but still access the implementation of your parent class by calling the special _super() method:

App.Person = Ember.Object.extend({
  say: function(thing) {
    var name = this.get('name');
    alert(name + ' says: ' + thing);
  }
});

App.Soldier = App.Person.extend({
  say: function(thing) {
    this._super(thing + ", sir!");
  },
  march: function(numberOfHours) {
    alert(this.get('name') + ' marches for ' + numberOfHours + ' hours.');
  }
});

var yehuda = App.Soldier.create({
  name: "Yehuda Katz"
});

yehuda.say("Yes");  // alerts "Yehuda Katz says: Yes, sir!"

The create() on line #17 creates an instance of the App.Soldier class. The extend() on line #8 creates a subclass of App.Person. Any instance of the App.Person class will not have the march() method.

You can also pass Mixin classes to add additional properties to the subclass.

App.Person = Ember.Object.extend({
  say: function(thing) {
    alert(this.get('name') + ' says: ' + thing);
  }
});

App.SingingMixin = Mixin.create({
  sing: function(thing){
    alert(this.get('name') + ' sings: la la la ' + thing);
  }
});

App.BroadwayStar = App.Person.extend(App.SingingMixin, {
  dance: function() {
    alert(this.get('name') + ' dances: tap tap tap tap ');
  }
});

The App.BroadwayStar class contains three methods: say(), sing(), and dance().

Parameters:

mixins [Mixin]
One or more Mixin classes
arguments [Object]
Object containing values to use within the new class
doc_EmberJs
2016-11-30 16:49:03
Comments
Leave a Comment

Please login to continue.