1

为了简洁起见,我简化了这个故事。

我有 2 个基本模型,用户和公司,并在单独的模板上显示每个模型的列表。但是在用户列表上,我希望在顶部有一个下拉列表来过滤可见的用户。现在,我只需要在对数据进行一些按摩后,在用户视图中的公司模型/控制器中的公司列表(我只需要公司名称列表,而不是下拉列表中的整个对象)。

我的路线:

App.Router.map(function(){
    this.resource("users");    
    this.resource("companies");
)};

用户模型路由和用户路由控制器:

App.User = DS.Model.extend({
    username: DS.attr('string'),
    company: DS.attr('string')
});

App.UsersRoute = Ember.Route.extend({
    model: function(){
        return this.store.find("User");
    }
});

App.UsersController = Ember.ArrayController.extend({        
    companyList: [],
    filterUserCompany: function(){
        console.log("filterUserCompany");
    },  
    count: Ember.computed.alias("length")    
});

公司模型、路线和控制器:

App.Company = DS.Model.extend({
    name: DS.attr('string'),
    address1: DS.attr('string'),
    address2: DS.attr('string'),
    city: DS.attr('string'),
    state: DS.attr('string'),
    postal: DS.attr('string'),
    country: DS.attr('string'),
    created: DS.attr('date')
});

App.CompaniesRoute = Ember.Route.extend({
    model: function(){
        return this.store.findAll("Company");
    }
});

App.CompaniesController = Ember.ArrayController.extend({
    count: Ember.computed.alias("length")
});

Users.htm 模板(顶部是公司下拉列表,我只想要公司名称,底部是用户列表)

<div class="inner-content-tools" style="width:auto;height:28px;">
    <div class="add-btn filterlist-item">Add user</div>
    <div class="filterlist-item">
        {{view Ember.Select
        id="filter_company"
        content=companyList
        selection=company
        class="selectlist"
        prompt="Show all users, or select company"
        change=filterUserCompany
        }}
    </div>
</div>
<br />    
    <table id="data-table" width="100%" cellspacing="0" style="margin-bottom:20px;">        
        {{#each}}
        <tr>
            <td>{{company}}</td>
            <td>{{username}}</td>
            <td>{{#link-to "user" this tagName="span"}}go to{{/link-to}}</td>
        </tr>
        {{/each}}
    </table>
</div>

我在 UsersRoute 中尝试了几个衍生品

setupController: function (controller, model) {
    this._super(controller, model);

    // THIS WORKS TO FILL DROP DOWN
    controller.set("companyList", ["ID Plans", "ReicheGroup"]);

    // THIS NEVER WORKS
    //this.controllerFor('companies').get('model');
    //
},

在获得公司模型(公司数组)之后,我想迭代它们并为 companyList 属性创建一个简单的列表数组,以填充用户模板中的下拉列表

我也在 UsersController 中尝试过很多衍生品

App.UsersController = Ember.ArrayController.extend({

    needs: "companies",

    companyList: function(){

         // AND ITERATE controllers.companies.model TO
         // GET THE COMPANY NAME (.name) FROM EACH COMPANY OBJECT
        return controllers.companies.model.ITERATE THESE AND RETURN ARRAY OF NAMES

    }.property(),

    filterUserCompany: function(){
        console.log("filterUserCompany");
    },

    count: Ember.computed.alias("length")

});

就我有两条正确显示模板的工作路线而言,一切正常,一条显示公司列表,一条显示用户列表(因此 restadapter 工作正常)。两个页面都显示和运行良好(mydomain.com/users 和 mydomain.com/companies),并分别深入到个人用户或公司。但是,我只想要用户页面上的公司列表,并且只进行 ajax 调用并获取公司列表(为什么不使用已经在商店中的公司)似乎很简单。

帮助我欧比,你是我唯一的希望。

4

0 回答 0