Mixins are a great way of reusing and sharing code in Ember. The following recipes go over some basic operations on how to use them in your code.
In this recipe, we'll create a common mixin object.
Create an Ember mixin object that has a couple of properties and a function:
const common = Ember.Mixin.create({ property1: 'This is a mixin property', edit: function() { console.log('Starting to edit'); this.set('isEditing', true); }, isEditing: false });
This mixin can be added to any object. For the sake of simplicity, all this mixin does is display some text and set the
isEditing
property totrue
if theedit
function is invoked.Let's see what it looks like when we add this object to an object:
const obj = Ember.Object.extend(common,{ objprop: 'This is an Ember object property' }); const object = obj.create();
The
extend
method present inEmber.Object
allows for one or more optional arguments of theEmber.Mixin
type. In this example, we added...