Ember.js provides relationship types to specify how the models are related to each other. There are different relationship types such as One-to-One relationship can be used with DS.belongsTo, One-to-Many relationship can be used with DS.hasMany along with DS.belongsTo and Many-to-Many relationship can be used with DS.hasMany.
Syntax
import DS from 'ember-data'; export default DS.Model.extend ({ var_name1: DS.belongsTo('model_name1'), var_name2: DS.hasMany('model_name2') });
Example
The example given below shows the use of relationship types. Create two adapters with the names account and staff by using the following command −
ember generate adapter adapter_name
Now open the app/adapters/account.js file and add the following code −
import ApplicationAdapter from './application'; //created an "account" array to store relationship data const account = { "data": { "type": "account", "id": "100", "relationships": { "secondVal": { "data": { "type": "staff", "id": "2" } }, "firstVal": { "data": { "type": "staff", "id": "1" } } } } }; export default ApplicationAdapter.extend ({ //this method fetches data from 'staff' adapter findRecord() { //returns the data from array return account; } });
Open the app/adapters/staff.js file and add the following code −
import ApplicationAdapter from './application'; import Ember from 'ember'; //values given for type and id const relval1 = { data: { type: "staff", id: "1", attributes: { name: 'JavaScript' } } }; const relval2 = { data: { type: "staff", id: "2", attributes: { name: 'jQuery' } } }; //the variable 'relval3' pushes the data to 'relval1' and 'relval2' const relval3 = Ember.A(); relval3.pushObject(relval1); relval3.pushObject(relval2); export default ApplicationAdapter.extend ({ findRecord(store, type, id) { //finds the item id and returns to 'relval3' variable let valret = relval3.find(function (item) { return id === Ember.get(item, 'data.id'); }); //the searched item will passed to 'relval3' from 'valret' variable return valret; } });
Create two models with the names account and staff. Open the app/models/account.js file and include the following code −
import DS from 'ember-data'; import Model from "ember-data/model"; import attr from "ember-data/attr"; //defines one-to-one and one-to-many relationship between models import { belongsTo, hasMany } from "ember-data/relationships"; export default DS.Model.extend({ //when async is 'true', it will fetch related entries firstVal: belongsTo('staff', {async: true}), secondVal: belongsTo('staff', {async: true}) });
Now open the app/models/staff.js file and include the following code −
import DS from 'ember-data'; import Model from "ember-data/model"; import attr from "ember-data/attr"; import { belongsTo, hasMany } from "ember-data/relationships"; export default DS.Model.extend ({ //specifying attributes using 'attr()' method name: attr() });
Next, create a route and name it as application.js. Open this file, which is created under app/routes/ and add the following code −
import Ember from 'ember'; export default Ember.Route.extend ({ model(){ //returns the value of model() hook return this.get('store').findRecord('account', 100); //retrieve a record for specific id } });
Create a serializer with the name application by using the following command −
ember generate serializer serializer_name
Open the app/serializers/application.js file and add the following code −
import DS from 'ember-data'; //it is the default serializer and works with JSON API backends export default DS.JSONAPISerializer.extend ({ //keyForRelationship() method overwrites the naming conventions keyForRelationship: function(key, relationship, method) { return Ember.String.camelize(key); //returns the lowerCamelCase form of a string } });
Open the application.hbs file created under app/templates/ with the following code −
<h2>Model Relationships</h2> //display the id along with the name {{model.firstVal.id}} : {{model.firstVal.name}} <br> {{model.secondVal.id}} : {{model.secondVal.name}} {{outlet}}
Output
Run the ember server; you will receive the following output −
Previous Page:-Click Here
I think this is a real great post.Much thanks again. Want more.
I’ve been absent for some time, but now I remember why I used to love this site. Thank you, I will try and check back more frequently. How frequently you update your web site?