Definition, initialization, inheritance of the Ember class


May 09, 2021 06:00 Ember.js Reference documents



Ember JS provides its own class system, JavaScript standard classes do not automatically update property values, Ember JS classes automatically trigger observers, automatically update property values, automatically refresh property values on templates. I f a class is provided by Ember JS, you can see that the prefix namespace Ember.Object Ember class definition extend() creation of class create() which can pass in parameters in the hash lists.

Ember JS rewrites the array class Array of the standard JavaScript and named Ember.Enumerable distinguish it from the standard JavaScript class (API introduction)

Ember JS also extends the nature String property to provide a unique set of treatments, described in the API.

The naming rules for classes are not introduced here, so find a Java naming rules textbook online.

Get ready before you start, first create an HTML file and introduce the files that Ember JS needs (a simpler way to EmberJS project methods is described later, although you can also learn about EmberJS projects Ember CLI if you have EmberJS

  1. Ember.js • Guides
  2. // 在这里编写Ember代码

The code above is a simple HTML requires the Ember to use the CDN

Class definition

Here's how to define a Person class:

  1. Person = Ember.Object.extend({
  2.   say(thing) {
  3.     alert(name);
  4.   }
  5. });

The above code defines a Person class, and also defines a method say class, which is passed in with a parameter thing The method body simply prints the incoming parameters.

Class inheritance

The method of the parent class is overridten in the child class, and _super() method is called in the method to trigger the behavior of the parent method by calling the corresponding method in the parent class.

  1. Person = Ember.Object.extend({
  2.     
  3.   say(thing) {
  4.     var name = this.get('name');
  5.     alert(name + " says: " + thing);
  6.   }
  7. });
  8. Soldier = Person.extend({
  9.     
  10.   say(thing) {
  11.     // this will call the method in the parent class (Person#say), appending
  12.     // the string ", sir!" to the variable `thing` passed in
  13.     this._super(thing + ", sir!");
  14.   }
  15. });
  16. var yehuda = Soldier.create({
  17.   name: "Yehuda Katz"
  18. });
  19. yehuda.say("Yes"); // alerts "Yehuda Katz says: Yes, sir!"

Run the code, refresh the browser, and you'll see the following results:

Definition, initialization, inheritance of the Ember class

The results are correct, but we don't yet know how the class was initialized, and what is the order in which it was initialized? I n fact, each class has a default initialization method, 555... Don't worry, look down.

Class instantiation

To get an instance of a class, you only need to call the create() method.

  1. Person = Ember.Object.extend({
  2. show() {
  3. console.log("My name is " + this.get('name'));
  4. }
  5. });
  6. var person = Person.create({
  7. name: 'ubuntuvim'
  8. });
  9. person.show(); // My name is ubuntuvim
  10. var person2 = Person.create({
  11. pwd: 'ubuntuvim'
  12. });
  13. // 由于创建person2的时候没有设置name的值,默认是undefined
  14. person2.show(); // My name is undefined

Definition, initialization, inheritance of the Ember class

Note: When creating an instance using the create() method for performance reasons, no new definitions, rewriting create() nor is it recommended for new definitions, rewriting normal methods, Ember recommends simply passing simple parameters when using create() such as {name: 'ubuntuvim'} If you need a new definition, rewrite method, create a new sub-class to implement.

Define create() within the create() method and run it directly to report the error information in the figure below.

  1. Person = Ember.Object.create({
  2. show() {
  3. console.log("My name is " + this.get('name'));
  4. },
  5. fullName: Ember.computed(function() {
  6. console.log("computed properties.");
  7. })
  8. });

Definition, initialization, inheritance of the Ember class

Class initialization

As mentioned earlier, how exactly the class is initialized when the class is inherited, this section describes the initialization of the class, Ember init() is called automatically when the class is instantiated.

  1. Parent = Ember.Object.extend({
  2. init() {
  3. console.log("parent init...");
  4. },
  5. show() {
  6. console.log("My name is " + this.get('name'));
  7. },
  8. others() {
  9. console.log("the method in parent class..");
  10. }
  11. });
  12. //parent = Parent.create({
  13. // name: 'parent'
  14. //});
  15. Child = Parent.extend({
  16. init() {
  17. console.log("child init...");
  18. },
  19. show() {
  20. this._super();
  21. }
  22. });
  23. child = Child.create({
  24. name: 'child'
  25. });
  26. child.show();
  27. child.others();

Note: init() is called automatically only when the create() method is called, and in the example above, if child.others() method parent class does not call the init() the init() method is executed only when Parent.create() init() The above code, if Parent.create() results in the following:

Definition, initialization, inheritance of the Ember class

You can see that init() is not called, then modify the code, comment out child.others() and then remove the comments for Parent.create() The following results are obtained

Definition, initialization, inheritance of the Ember class

You can see that the init() method is called! T his shows init() method is called only when the create() method is called. In a project where it is possible that you need to inherit components provided by Ember such as Ember.Component class, you should be aware that when you inherit the components of Ember you must this._super() the class you Component or get unpredictable results.

The access to the class property

Ember accessing the properties of the class get、set method. You can also obj.prop but if you don't use the accessor, you can cause a lot of problems: calculated properties can't be recalculated, object properties can't be detected, templates can't be updated automatically.

  1. Person = Ember.Object.extend({
  2. name: 'ubuntuvim'
  3. });
  4. // Ember 推荐的访问方式
  5. var person = Person.create();
  6. console.log("My name is " + person.get('name'));
  7. person.set('name', "Tobias Funke");
  8. console.log("My name is " + person.get('name'));
  9. console.log("---------------------------");
  10. // 不推荐的方式
  11. var person2 = Person.create();
  12. console.log("My name is " + person2.name);
  13. person2.name = "Tobias Funke";
  14. console.log("My name is " + person2.name);

Ember encapsulates get、set details for us, and developers can use them directly.
Finally, THENKS to the 靑 of the gaze.
The full code of the blog post is placed in Github (the blog post has been modified several times, and the code on the blog post may be different from the github code, but the impact is small!). I f you think the blog post is a bit of a github project for you, give me star Yours is definitely the biggest motivation for me!!