CoreObject#reopenClass()

reopenClasspublic

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

Augments a constructor's own properties and functions:

1
2
3
4
5
6
7
8
9
10
MyObject = Ember.Object.extend({
  name: 'an object'
});
 
MyObject.reopenClass({
  canBuild: false
});
 
MyObject.canBuild; // false
o = MyObject.create();

In other words, this creates static properties and functions for the class. These are only available on the class and not on any instance of that class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
App.Person = Ember.Object.extend({
  name : "",
  sayHello : function() {
    alert("Hello. My name is " + this.get('name'));
  }
});
 
App.Person.reopenClass({
  species : "Homo sapiens",
  createPerson: function(newPersonsName){
    return App.Person.create({
      name:newPersonsName
    });
  }
});
 
var tom = App.Person.create({
  name : "Tom Dale"
});
var yehuda = App.Person.createPerson("Yehuda Katz");
 
tom.sayHello(); // "Hello. My name is Tom Dale"
yehuda.sayHello(); // "Hello. My name is Yehuda Katz"
alert(App.Person.species); // "Homo sapiens"

Note that species and createPerson are not valid on the tom and yehuda variables. They are only valid on App.Person.

To add functions and properties to instances of a constructor by extending the constructor's prototype see reopen

doc_EmberJs
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.