我是 EmberJS 的新手。我正在尝试制作一个包含两个实体(用户和部门)的简单应用程序。我为用户 CRUD 和部门 CRUD 有单独的模板和路由。
这是有关我的模型的详细代码
App.ApplicationAdapter = DS.RESTAdapter.extend();
App.Store = DS.Store.extend({
adapter: 'App.ApplicationAdapter'
});
DS.RESTAdapter.reopen({
host: 'http://localhost:8081/slim'
});
App.Department = DS.Model.extend({
departmentname : DS.attr(),
user : DS.hasMany('user')
});
App.User = DS.Model.extend({
username : DS.attr('string'),
full_name : DS.attr(),
email : DS.attr(),
department_id : DS.belongsTo('department')
});
在阅读模式下,我可以使用它来显示用户所在的部门
Department: {{department_id.departmentname}}
但是,在用户编辑/用户创建模式下,我想在此处显示一个显示部门的下拉列表。 我不知道如何执行此操作,因为在定义“用户”路由时,我将模型指定为 App.User。
我尝试过,但没有成功
{{view Ember.Select content=model.departments}}
我的代码使用 PHP Rest api,因此它不会在 jsbin 上运行。但我还是放了链接
最佳答案
这在较新版本的 ED(1.0 beta 1+)中不是必需的
App.Store = DS.Store.extend({
adapter: 'App.ApplicationAdapter'
});
DS.RESTAdapter.reopen({
host: 'http://localhost:8081/slim'
});
应该是这样的
App.ApplicationAdapter = DS.RESTAdapter.extend({
host: 'http://localhost:8081/slim'
});
只需向 Controller 添加一个属性并获取部门
App.UserController = Ember.ObjectController.extend({
departments: function(){
return this.store.find('department');
}.property(),
isEditing : false,
edit: function(){
this.set('isEditing', true);
},
doneEditing: function(){
this.set('isEditing', false);
var user = this.get('model');
user.save();
}
});
然后在您的用户模板中
{{view Ember.Select
content=departments
optionValuePath="content.id"
optionLabelPath="content.departmentname"}}
http://emberjs.com/api/classes/Ember.Select.html
PS 这是关于 Ember Data 1.0 beta+ 更改的有用简短阅读 https://github.com/emberjs/data/blob/master/TRANSITION.md
关于ember.js - EmberJS 从另一个模型填充下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20925601/