1

我正处于学习 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);
        }   
    }
);  

那么这是如何在 ember 中完成的呢?

编辑 :

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"
    }), }
4

1 回答 1

1

您可以使用needsAPI 从您的特定控制器访问其他控制器。

needs: 'manager',
managerBinding: 'controllers.manager'

从那里,如果manager是你想要的对象,你可以通过this.get('manager')控制器和manager相应的模板引用它。

于 2013-07-21T05:35:25.300 回答