Coding With Fun
Home Docker Django Node.js Articles FAQ

Extension of the Ember class


May 09, 2021 Ember.js Reference documents



Extend general properties

reopen not know how to translate well, reopen translation 扩展 should be "reopened", but always feel bad, so translated into an extension, if there is something wrong please point out.

When you want to extend a class you can add reopen() directly to a defined class using the reopen() method. I f you are extend() method you need to redefine a sub-class and then add new properties and methods to the sub-class. A s noted in the previous article, create() be called with a calculated property and is not recommended for new definitions and reopen() method can compensate for the create() method. Much extend() approach, the following code demonstrates their differences.

  1. Parent = Ember.Object.extend({
  2. name: 'ubuntuvim',
  3. fun1() {
  4. console.log("The name is " + this.name);
  5. },
  6. common() {
  7. console.log("common method...");
  8. }
  9. });
  10. // 使用extend()方法添加新的属性、方法
  11. Child1 = Parent.extend({
  12. // 给类Parent为新增一个属性
  13. pwd: '12345',
  14. // 给类Parent为新增一个方法
  15. fun2() {
  16. console.log("The pwd is " + this.pwd);
  17. },
  18. // 重写父类的common()方法
  19. common() {
  20. //console.log("override common method of parent...");
  21. this._super();
  22. }
  23. });
  24. var c1 = Child1.create();
  25. console.log("name = " + c1.get('name') + ", pwd = " + c1.get('pwd'));
  26. c1.fun1();
  27. c1.fun2();
  28. c1.common();
  29. console.log("-----------------------");
  30. // 使用reopen()方法添加新的属性、方法
  31. Parent.reopen({
  32. // 给类Parent为新增一个属性
  33. pwd: '12345',
  34. // 给类Parent为新增一个方法
  35. fun2() {
  36. console.log("The pwd is " + this.pwd);
  37. },
  38. // 重写类本身common()方法
  39. common() {
  40. console.log("override common method by reopen method...");
  41. //this._super();
  42. },
  43. // 新增一个计算属性
  44. fullName: Ember.computed(function() {
  45. console.log("compute method...");
  46. })
  47. });
  48. var p = Parent.create();
  49. console.log("name = " + p.get('name') + ", pwd = " + p.get('pwd'));
  50. p.fun1();
  51. p.fun2();
  52. p.common();
  53. console.log("---------------------------");
  54. p.get('fullName'); // 获取计算属性值,这里是直接输出:compute method...
  55. // 使用extend()方法添加新的属性、方法
  56. Child2 = Parent.extend({
  57. // 给类Parent为新增一个属性
  58. pwd: '12345',
  59. // 给类Parent为新增一个方法
  60. fun2() {
  61. console.log("The pwd is " + this.pwd);
  62. },
  63. // 重写父类的common()方法
  64. common() {
  65. //console.log("override common method of parent...");
  66. this._super();
  67. }
  68. });
  69. var c2 = Child2.create();
  70. console.log("name = " + c2.get('name') + ", pwd = " + c2.get('pwd'));
  71. c2.fun1();
  72. c2.fun2();
  73. c2.common();

Extension of the Ember class

The difference can be seen from the execution results as follows:
Same point: Can be used to extend a class.
Alien:

  1. extend needs to redefine a class and inherit the extended class;
  2. reopen a new property, method, that is added to the extended class itself, and can extend the calculated property create() method);

Depending on the actual situation with that method, reopen changes the behavior of the original class (it is conceivable that the method and properties of the object's prototype object are modified), just as the behavior of the reopen class common method Child2 by the common has changed, and the reopen method may have reopen know what was going on before the encoding process forgot! If the extend results in more classes, the inheritance tree becomes deeper and deeper, which is also a challenge for performance and extend not change the behavior of the inherited class.

Extend static properties

Use reopenClass() extend the properties and methods of the static type.

  1. Parent = Ember.Object.extend();
  2. // 使用reopenClass()方法添加新的static属性、方法
  3. Parent.reopenClass({
  4. isPerson: true,
  5. username: 'blog.ddlisting.com'
  6. //,name: 'test' //这里有点奇怪,不知道为何不能使用名称为name定义属性,会提示这个是自读属性,使用username却没问题!!估计name是这个方法的保留关键字
  7. });
  8. Parent.reopen({
  9. isPerson: false,
  10. name: 'ubuntuvim'
  11. });
  12. console.log(Parent.isPerson);
  13. console.log(Parent.name); // 输出空
  14. console.log(Parent.create().get('isPerson'));
  15. console.log(Parent.create().get('name')); // 输出 ubuntuvim

There is an explanation for the address below the problem name the property name in the reopenClass method

  1. http://discuss.emberjs.com/t/reopenclass-method-can-not-pass-attribute-named-name-of-it/10189
  2. http://stackoverflow.com/questions/36078464/reopenclass-method-can-not-pass-attribute-named-name-of-it

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!!