javascript - Ember DRY pattern for reusing "Ember.computed.alias" -


i have form transitions through several views. each controller.js file has long list of these ember.computed.alias. how can break out 1 file , import each controller?

currently in each controller.js

entityemail: ember.computed.alias('controllers.checkout.entityemail'), entitydob: ember.computed.alias('controllers.checkout.entitydob'), entityphone: ember.computed.alias('controllers.checkout.entityphone'), entityaddress1: ember.computed.alias('controllers.checkout.entityaddress1'), entityaddress2: ember.computed.alias('controllers.checkout.entityaddress2'), entitycity: ember.computed.alias('controllers.checkout.entitycity'), 

i pull out file can import 1 liner in each controller.js

this classic use-case ember.mixin.
can extract these computed props single mixin , extend every controller (that needs have these props) it.

add following mixin app

// app/mixins/entity-form.js  import ember 'ember';  const { mixin, inject, computed: { alias } } = ember;  export default mixin.create({    checkout: inject.controller(),    entityemail: alias('checkout.entityemail'),   entitydob: alias('checkout.entitydob'),   entityphone: alias('checkout.entityphone'),   entityaddress1: alias('checkout.entityaddress1'),   entityaddress2: alias('checkout.entityaddress2'),   entitycity: alias('checkout.entitycity')  }); 

and use in controller

// app/controllers/example.js  import entityformmixin 'yourappname/mixins/entity-form';  const { controller } = ember;  export default controller.extend(entityformmixin, {   // rest of controller's props , functions }); 

note: ember.inject api available since ember 1.10.0. in case using older version need replace inject line with: needs: ['checkout'] , prefix aliases "controllers." did in example.


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -