WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
×


 

EmberJS Relationships


Ember.js gives relationship types to determine how the models are identified with one another. There are diverse relationship types, for example, One-to-One relationship can be utilized with DS.belongsTo, One-to-Many relationship can be utilized with DS.hasMany alongside DS.belongsTo and Many-to-Many relationship can be utilized with DS.hasMany.

Syntax

 

import DS from 'coal information'; 

trade default DS.Model.extend ({ 

var_name1: DS.belongsTo('model_name1'), 

var_name2: DS.hasMany('model_name2') 

}); 

Example

The model given underneath demonstrates the utilization of relationship types. Make two connectors with the names account and staff by utilizing the accompanying direction −

 

ash create connector adapter_name 

Now open the app/connectors/account.js document and include the accompanying code −

 

import ApplicationAdapter from './application'; 

/made an "account" cluster to store relationship information 

const account = { 

"information": { 

"type": "account", 

"id": "100", 

"connections": { 

"secondVal": { 

"information": { 

"type": "staff", 

"id": "2" 

} 

}, 

"firstVal": { 

"information": { 

"type": "staff", 

"id": "1" 

} 

} 

} 

} 

}; 

send out default ApplicationAdapter.extend ({ 

/this strategy brings information from 'staff' connector 

findRecord() { 

/restores the information from cluster 

return account; 

} 

}); 

Open the app/connectors/staff.js document and include the accompanying code −

 

import ApplicationAdapter from './application'; 

import Ember from 'ash'; 

/values given for sort and id 

const relval1 = { 

information: { 

type: "staff", 

id: "1", 

qualities: { 

name: 'JavaScript' 

} 

} 

}; 

const relval2 = { 

information: { 

type: "staff", 

id: "2", 

qualities: { 

name: 'jQuery' 

} 

} 

}; 

/the variable 'relval3' drives the information to 'relval1' and 'relval2' 

const relval3 = Ember.A(); 

relval3.pushObject(relval1); 

relval3.pushObject(relval2);
send out default ApplicationAdapter.extend ({ 

findRecord(store, type, id) { 

/finds the thing id and comes back to 'relval3' variable 

let valret = relval3.find(function (thing) { 

return id === Ember.get(item, 'data.id'); 

}); 

/the looked thing will go to 'relval3' from 'valret' variable 

return valret; 

} 

}); 

Create two models with the names account and staff. Open the app/models/account.js record and incorporate the accompanying code −

 

import DS from 'coal information'; 

import Model from "coal information/show"; 

import attr from "coal information/attr"; 

/characterizes balanced and one-to-numerous connection between models 

import { belongsTo, hasMany } from "coal information/connections"; 

trade default DS.Model.extend({ 

/when async is 'genuine', it will get related passages 

firstVal: belongsTo('staff', {async: true}), 

secondVal: belongsTo('staff', {async: true}) 

}); 

Now open the app/models/staff.js record and incorporate the accompanying code −

 

import DS from 'coal information'; 

import Model from "coal information/show"; 

import attr from "ash information/attr"; 

import { belongsTo, hasMany } from "ash information/connections"; 

send out default DS.Model.extend ({ 

/determining traits utilizing 'attr()' technique 

name: attr() 

}); 

Next, make a course and name it as application.js. Open this document, which is made under app/courses/ and include the accompanying code −

 

import Ember from 'ash'; 

send out default Ember.Route.extend ({ 

model(){ 

/restores the estimation of model() snare 

return this.get('store').findRecord('account', 100);/recover a record for explicit id 

} 

}); 

Create a serializer with the name application by utilizing the accompanying order −

 

ash create serializer serializer_name 

Open the app/serializers/application.js document and include the accompanying code −

 

import DS from 'ash information'; 

/it is the default serializer and works with JSON API backends 

send out default DS.JSONAPISerializer.extend ({ 

/keyForRelationship() strategy overwrites the naming traditions 

keyForRelationship: function(key, relationship, strategy) { 

return Ember.String.camelize(key);/restores the lowerCamelCase type of a string 

} 

}); 

Open the application.hbs document made under application/formats/with the accompanying code −

 

<h2>Model Relationships</h2> 

/show the id alongside the name 

{{model.firstVal.id}} : {{model.firstVal.name}} 

<br> 

{{model.secondVal.id}} : {{model.secondVal.name}} 

{{outlet}}