我正处于学习ember.js的初始阶段。我正在尝试一个应用程序,并且在从控制器中存在的对象引用另一个对象时卡住了。
{{#each employee in controller}}
<tr>
<td class="span2"><input type="checkbox" style="margin-left : 110px;"/></td>
<td class="span2">{{#linkTo 'employee' employee}}{{employee.empId}}{{/linkTo}}</td>
<td class="span2">{{employee.name}}</td>
<td class="span2">{{employee.age}}</td>
<td class="span2">{{employee.dob}}</td>
<td class="span2">{{employee.gender}}</td>
<td class="span2">{{employee.doj}}</td>
<td class="span2">{{#linkTo 'employee' employee}}{{employee.mgrId}}{{/linkTo}}</td>
<td class="span2">{{employee.package}}</td>
<td class="span2">{{employee.address}}</td>
<td class="span2">{{employee.phone}}</td>
<td> <button {{action "deleteEmployee" employee}}> Delete </button></td>
{{/each}}
这里我试图使用id ..
来获取经理的详细信息索引路由的代码是:
App.Router.map(function() {
this.resource('employee',{
path: "/employee/:employee_id"},
function() {
this.route('edit');
}
);
this.route('allEmployees');
// put your routes here
});
App.AllEmployeesRoute = Ember.Route.extend({
model: function() {
//console.log(param.patient_id);
var employees = [];
for(var employee_id in App.Employees)
{
employees.push(App.Employees[employee_id])
}
return employees;
}
});
App.Employee = Ember.Object.extend({
"empId":null,
"name":null,
"age":null,
"dob":null,
"gender":null,
"doj":null,
"mgrId":null,
"package":null,
"address":null,
"phone":null
});
// Code for event handling
App.AllEmployeesController = Ember.ArrayController.extend({
deleteEmployee:function(employee){
var employees = this.get('model');
for(var i=0; i<employees.length;i++)
{
if(employees[i].empId === employee.empId)
employees.removeAt(i);
}
console.log("deleting "+employee.empId);
}
}
);
那么如何在余烬中完成?
编辑:
JSON记录结构:
App.Employees = {
"1" : App.Employee.create({
"id" : "1",
"empId" : "emp1",
"name" : "Employee 1",
"age" : "23",
"dob" : "17/12/89",
"gender" : "male",
"doj" : "9/1/12",
"mgrId" : "emp6",
"package" : "300000",
"address" : "address 1",
"phone" : "9999999999"
}),
"2" : App.Employee.create({
"id" : "2",
"empId" : "emp2",
"name" : "Employee 2",
"age" : "24",
"dob" : "18/1/90",
"gender" : "female",
"doj" : "20/2/13",
"mgrId" : "",
"package" : "350000",
"address" : "address 2",
"phone" : "9437543985"
}),
"3" : App.Employee.create({
"id" : "3",
"empId" : "emp3",
"name" : "Employee 3",
"age" : "25",
"dob" : "19/2/91",
"gender" : "male",
"doj" : "12/12/11",
"mgrId" : "emp2",
"package" : "300000",
"address" : "address 3",
"phone" : "3454395943"
}), }
答案 0 :(得分:1)
您可以使用needs
API访问特定控制器中的其他控制器。
needs: 'manager',
managerBinding: 'controllers.manager'
从那里,如果manager
是您想要的对象,则可以通过控制器中的this.get('manager')
和相应模板中的manager
引用它。