ember js关联访问后端nodejs mongoose [英] ember js association access backend nodejs mongoose
问题描述
用户可以属于许多组织,组织可以拥有很多用户。用户可以是管理员或客户。这是我在后端建模的方式:
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:'用户'}],
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,
// organization:[{type:Schema.Types.ObjectId,ref:'Organization'}]
});
module.exports = mongoose.model('User',UserSchema);
那么我有CRUD的用户。我创建了一些将一些用户ID存储为org_admins,另外一些用作org_customers的组织。
我想创建一个索引视图,所有组织都有相应的管理员和相应的客户。
这是现在浏览器中基本模板的显示方式:
组织:
Org1地址1
管理员:
5534a5172751c5b05b7e7cd0
5534cb359262868030211796
客户:
org2 address2
管理员:
5534a5172751c5b05b7e7cd0
客户:
5534cb359262868030211796
我想要显示客户的管理员和用户名的用户名。
这是Ember侧代码:
app / models / organization.js
从ember-data导入DS;
导出默认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
/ p>
从ember-data导入DS;
var User = DS.Model.extend({
username:DS.attr('string'),
password:DS.attr('string'),
role:DS.attr('string')
});
导出默认用户;
这是模板文件 app / templates / organization.hbs
< ul>
组织:
< p>
{{#each organization in model}}
< li>
< label> {{organization.org_name}}< / label>
< label> {{organization.org_address}}< /标签>
< p> <标签>管理员:< / label>< / p>
< ul>
{{#each admin in organization.org_admins}}
< li> {{admin}}< / li>
{{/ each}}
< / ul>
< p> <标签>客户:< / label>< / p>
< ul>
{{#each client.org_customers中的客户}}
< li> {{customer}}< / li>
{{/ each}}
< / ul>
< / li>
{{/ each}}
< / p>
< / ul>
< p>
< button {{action'neworganization'}}>添加组织< / button>
< / p>
{{outlet}}
路线文件
应用/routes/organizations.js
从ember导入Ember;
导出默认值Ember.Route.extend({
model:function(){
return this.store.find('organization');
}
});
我继续在控制器中获取用户名,但没有工作。
我明白我在建立关联中做错了事情。我也尝试使用DS.hasMany而不是DS.attr(),但是无法实现我的动机,我可能会做错事,不能正确理解关联。
我可以做什么正确的方法?请帮助。
请参阅指南中定义相关性的部分 http://guides.emberjs.com/v1.11.0/models/defining-models/
当您使用 DS.hasMany
时,需要相应的 DS.belongsTo
另一个需要注意的事情是确保您在组织的响应有效载荷中包含所有相应的用户模型,或者标记与 {async:true}
所以ember知道如果需要它们分别检索这些记录。
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);
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
I want the username of the admins and username of the customers to be displayed.
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;
This is the template file 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}}
The routes file app/routes/organizations.js
import Ember from 'ember';
export default Ember.Route.extend({
model: function(){
return this.store.find('organization');
}
});
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.
Take a look at the section on defining releationships in the guide http://guides.emberjs.com/v1.11.0/models/defining-models/
When you use DS.hasMany
you need a corresponding DS.belongsTo
on the other model.
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屋!