ember.js - What's difference between Mixins and Services on Emberjs -
i'm trying follow best practice ember , potentiality , question comes mind difference between mixins , services , how using each of them?
i have services/mixins , work pretty want sure i'm doing right.
a mixin when want different objects have same behaviour/data. want several controllers trigger same action, change 1 argument:
// app/mixins/change-name.js export default ember.mixin.create({ actions: { changename(item) { item.set('name', this.get('name')); } } }); // app/controllers/some-controller import changename '<app-name>/mixins/change-name'; export default ember.controller.extend(changename, { name: 'some controller' });
notice controllers have same action, it's not shared, each has own. can extend mixins object since they're added _super()
chain.
services can seen sort of mutable shared data. if have set of data or behaviour needs accessed different parts of application, it's candidate service.
one such example shopping basket, example. regardless of in application, need refer same shopping basket in order manipulate data.
Comments
Post a Comment