- EmberJS 教程
- EmberJS - 主页
- EmberJS - 概览
- EmberJS - 安装
- EmberJS - 核心概念
- 创建和运行应用程序
- EmberJS - 对象模型
- EmberJS - 路由器
- EmberJS - 模板
- EmberJS - 组件
- EmberJS - 模型
- EmberJS - 管理依赖项
- EmberJS - 应用程序顾虑
- EmberJS - 配置 Ember.js
- EmberJS - Ember 检查器
- EmberJS 有用资源
- EmberJS - 快速指南
- EmberJS - 有用资源
- EmberJS - 讨论
EmberJS - 关系
Ember.js 提供关系类型来指定模型之间的相互关联方式。关系类型多种多样,例如,单向一对一关系可用 DS.belongsTo,单向一对多关系可用 DS.hasMany 结合 DS.belongsTo,多对多关系可用 DS.hasMany。
语法
import DS from 'ember-data'; export default DS.Model.extend ({ var_name1: DS.belongsTo('model_name1'), var_name2: DS.hasMany('model_name2') });
示例
以下所示示例展示了关系类型如何使用。使用以下命令创建两个名为账户和员工的适配器 −
ember generate adapter adapter_name
现在打开 app/adapters/account.js 文件并添加以下代码 −
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; } });
打开 app/adapters/staff.js 文件并添加以下代码 −
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; } });
创建两个名为账户和员工的模型。打开 app/models/account.js 文件并包含以下代码 −
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}) });
现在打开 app/models/staff.js 文件并包含以下代码 −
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() });
接下来,创建一个路由并将其命名为 application.js。打开此文件,该文件创建在 app/routes/ 下并添加以下代码 −
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 } });
使用以下命令创建一个名为应用程序的序列化器 −
ember generate serializer serializer_name
打开 app/serializers/application.js 文件并添加以下代码 −
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 } });
打开 app/templates/ 下创建的 application.hbs 文件并添加以下代码 −
<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}}
输出
运行 ember 服务器;您将收到以下输出 −
emberjs_model.htm
广告