- Ember.js Cookbook
- Erik Hanchett
- 770字
- 2021-07-16 12:58:02
Using mixins
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.
How to do it...
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 the common mixin to the newEmber.Object
object. We then instantiated this Ember object usingcreate
. - All that's left is to output the contents. Use
console.log
to display each property:console.log(object.get('objprop')); //This is an Ember object property console.log(object.get('property1')); //This is a mixin property console.log(object.get('isEditing')); //false object.edit(); //Starting to edit console.log(object.get('isEditing')); //true
This is what the output will look like. As you can see, we can access any of the mixin properties or methods as if the mixin was included in the Ember object itself. This is a convenient way of reusing code in your applications.
- Let's create another mixin:
const secondMixin = Ember.Mixin.create({ secondProperty: 'This is the second mixin property' });
- Now let's see how this looks if we add it to an Ember object:
const obj = Ember.Object.extend(common,secondMixin,{ objprop: 'This is an Ember object Property' });
- Now, we can have access to both the common and
secondMixin
in our object. We can useconsole.log
to outputsecondProperty
:console.log(object.get('secondProperty'));//This is the second mixin propety
Mixins with the Ember CLI
Mixins work very well with the Ember CLI. To start, use the mixin generator to create one.
- Make sure that you're in the application directory, and then type the following command:
$ ember generate mixin common installing mixin create app/mixins/common.js installing mixin-test create tests/unit/mixins/common-test.js
The
generator
command creates anapp/mixins
folder and thecommon.js
file. Thecommon.js
file is where we will put the code for the mixin. - We'll use the mixin from the previous example and add it to this file:
// app/mixins/common.js import Ember from 'ember'; export default Ember.Mixin.create({ property1: 'This is a mixin property', edit: function() { console.log('Starting to edit'); this.set('isEditing', true); }, isEditing: false });
This mixin is exactly the same as the previous example; however, now it's in a module that we can import anywhere, including components or controllers.
For now, we'll import it to our
app.js
file in theapp
folders directory. - First, we'll need to add the
import
statement to the top of the file:import common from './mixins/common';
This allows us to use the common mixin anywhere in the
app.js
file. - We'll add the following code to the bottom of the
app/app.js
file:// app/app.js const obj = Ember.Object.extend(common,{ objprop: 'This is an Ember object property' }); const object = obj.create(); console.log(object.get('objprop'));//This is an Ember object property console.log(object.get('property1'));//This is a mixin property console.log(object.get('isEditing'));//false object.edit(); //Starting to edit console.log(object.get('isEditing')); //true
As you can see, all the properties and methods in the common mixin are available to the object.
- If we were to add the common mixin to a component, it might look like following code. Add this code to the
common-example.js
file:// app/components/common-example.js import Ember from 'ember'; import common from '../mixins/common'; export default Ember.Component.extend(common,{ compprop: 'This is a component property', actions: { pressed: function(){ this.edit(); } } });
As always, we must first import the mixin to our component. The path is always relative to the directory you're in, therefore, we must use
../mixins/common
to find it.In the component, I added a simple action called
pressed
that triggers the mixinedit
method. If the action gets triggered, we would see theStarting to edit message
in the console. Look for more examples of components in Chapter 6, Ember Components.
How it works...
The Ember.Mixin
class allows the creation of mixins whose properties and methods can be added to other classes. They can't be instantiated but they can be added or mixed in.
A mixin in computer science is a class that lends or copies it's behavior to a borrowing class using composition instead of inheritance. It encourages code reuse and avoids ambiguity that multiple inheritance can cause.
- Drupal 8 Blueprints
- LabVIEW Graphical Programming Cookbook
- Learning Data Mining with Python
- C語言程序設計立體化案例教程
- Oracle Database 12c Security Cookbook
- 微信小程序項目開發實戰
- 匯編語言編程基礎:基于LoongArch
- Scrapy網絡爬蟲實戰
- 高性能MVVM框架的設計與實現:San
- INSTANT Lift Web Applications How-to
- 面向對象分析與設計(第3版)
- 生成藝術:Processing視覺創意入門
- Visual Basic.NET程序設計
- 軟件測試實驗實訓指南
- Building Probabilistic Graphical Models with Python