Quantcast
Channel: Ember.JS - Latest topics
Viewing all articles
Browse latest Browse all 4830

Dealing with calling `_super()` with template pattern for less coupled code?

$
0
0

@rrh wrote:

Today I finished Chapter 6 Acquiring behaviour through inheritance from Practical Object-Oriented Design in Ruby by Sandi Metz. I'm wondering how I could use the template pattern in init() hook.

The original solution is:

// app/components/accounts/members/add-new-member/component.js
init() {
  this._super();
  this.set('memberObject', Ember.Object.create());
  this.set('memberObject.role', this.get('defaultRole'));
},

For now the component is a concrete class (no inheritance involved). I can move the behaviour from init() like:

// app/components/accounts/members/add-new-member/component.js
init() {
  this._super();
  this.postInit();
},

postInit() {
  this.set('memberObject', Ember.Object.create());
  this.set('memberObject.role', this.get('defaultRole'));
}

I could go one step further and extend a Component or Object class with postInit:

// app/components/shared/component.js
init() {
  this._super();
  this.postInit();
},

postInit() {
  // implemented in sub-classes
}

// app/components/shared/component.js
postInit() {
  this.set('memberObject', Ember.Object.create());
  this.set('memberObject.role', this.get('defaultRole'));
}

Would it make it more bullet-proof or make it art for the art's sake?

Posts: 2

Participants: 2

Read full topic


Viewing all articles
Browse latest Browse all 4830

Trending Articles