ember js 关联访问后端 nodejs mongoose [英] ember js association access backend nodejs mongoose
问题描述
我是 Ember js/node js/mongo db 的菜鸟,发现很难实现一个简单的案例.我使用 nodejs 和 mongoose 作为后端,使用 emberjs 作为前端.要求是:
I am a noob in Ember js/node js/mongo db, and find hard to implement a simple case. I am using nodejs with mongoose as the backend, and emberjs for front end. The requirement is:
一个用户可以属于多个组织,一个组织可以有多个用户.用户可以是管理员或客户.这就是我在后端建模的方式:
A user can belong to many organizations and an organization can have many users. A user can be admin or a customer. This is how I have modelled this in the backend:
models/organization.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var OrganizationSchema = new Schema({
org_name: String,
org_address: String,
org_admins: [{ type: Schema.Types.ObjectId, ref: 'User' }],
org_customers: [{ type: Schema.Types.ObjectId, ref: 'User' }]
});
module.exports = mongoose.model('Organization',OrganizationSchema);
models/user.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
username: {type: String, unique: true},
password: String,
role: String,
//organizations: [{type: Schema.Types.ObjectId, ref: 'Organization'}]
});
module.exports = mongoose.model('User',UserSchema);
然后我有用户的 CRUD.我创建了一些组织,将一些用户 ID 存储为 org_admins,将一些存储为 org_customers.
then I have CRUD for Users. I created some organizations storing some users ids as org_admins, and some as org_customers.
我想创建一个索引视图,所有组织都出现在其中,以及相应的管理员和相应的客户.
I want to create an index view, where all the organizations come up, with corresponding admins and corresponding customers.
现在浏览器上的基本模板是这样的:
This is how the basic template appears on the browser now:
Organizations:
Org1 address1
admins:
5534a5172751c5b05b7e7cd0
5534cb359262868030211796
Customers:
org2 address2
admins:
5534a5172751c5b05b7e7cd0
customers:
5534cb359262868030211796
我希望显示管理员的用户名和客户的用户名.
这是 Ember 端代码:
This is the Ember side code:
app/models/organization.js
import DS from 'ember-data';
export default DS.Model.extend({
org_name: DS.attr('string'),
org_address: DS.attr('string'),
org_admins: DS.attr(),
org_customer: DS.attr()
});
app/models/user.js
import DS from 'ember-data';
var User = DS.Model.extend({
username: DS.attr('string'),
password: DS.attr('string'),
role: DS.attr('string')
});
export default User;
这是模板文件app/templates/organization.hbs
<ul>
Organizations:
<p>
{{#each organization in model}}
<li>
<label> {{organization.org_name}} </label>
<label> {{organization.org_address}}</label>
<p> <label> admins: </label></p>
<ul>
{{#each admin in organization.org_admins}}
<li>{{admin}}</li>
{{/each}}
</ul>
<p> <label> Customers: </label></p>
<ul>
{{#each customer in organization.org_customers}}
<li>{{customer}}</li>
{{/each}}
</ul>
</li>
{{/each}}
</p>
</ul>
<p>
<button {{action 'neworganization'}} > Add Organization </button>
</p>
{{outlet}}
路由文件app/routes/organizations.js
The routes file app/routes/organizations.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function(){
return this.store.find('organization');
}
});
我继续在控制器中获取用户名,但是没有用.我知道我在对关联建模时做错了.我也尝试使用 DS.hasMany 而不是 DS.attr(),但无法实现我的动机,我可能做错了什么,没有正确理解关联.
I proceeded by getting the username in the controller, but it didnt work. I understand I am doing something wrong in modelling the associations. I also tried using DS.hasMany instead of DS.attr(), but could not achieve my motive, I may be doing something wrong, not understanding the associations properly.
我可以这样做的正确方法是什么.请帮忙.
What is the right way I could do this. Please help.
推荐答案
查看指南中关于定义关系的部分 http://guides.emberjs.com/v1.11.0/models/defining-models/
Take a look at the section on defining releationships in the guide http://guides.emberjs.com/v1.11.0/models/defining-models/
当您使用 DS.hasMany
时,您需要在另一个模型上使用相应的 DS.belongsTo
.
When you use DS.hasMany
you need a corresponding DS.belongsTo
on the other model.
另一件需要注意的事情是确保您在组织响应负载中包含所有相应的用户模型,或者使用 {async: true}
标记关系,以便 ember 知道检索这些如果需要,请单独记录.
Another thing to watch out for would be making sure you either include all the corresponding user models in your organisation response payload, or mark the relationship with {async: true}
so ember knows to retrieve those records separately if it needs them.
这篇关于ember js 关联访问后端 nodejs mongoose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!