Ember - trying to access CompaniesController model data from UsersController or UsersRoute

21 Views Asked by At

I have simplified the story for brevity.

I have 2 basic models, User and Company and display lists of each on separate templates. But on the Users list I want to have a drop down at top to filter what users are visible. For now I just want the list of companies from the Company model/controller in the User view after some massaging of data (I only need list of company names not entire object in drop down).

My Routes:

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

User model route and controller for users route:

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")    
});

Company model, route, and controller:

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 template (at top is company drop down where I only want company name and at bottom is list of users)

<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>

I have tried several derivatives of this in the 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');
    //
},

And after I get the companies model (an array of companies) I want to iterate them and make a simple list array for the companyList property to fill the drop down in the Users template

I have also tried many derivative of this in the 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")

});

Everything works fine as far as I have two working routes that display templates properly, one shows list of companies and one shows list of users (so restadapter is working fine). Both pages display and work great (mydomain.com/users and mydomain.com/companies) as well as drill down to individual user or company respectively. But, I just want a friggin list of companies on the users page and it seemed non-emberesque to just do an ajax call and get list of companies (why not use the companies already in store).

Help me Obi-won you're my only hope.

0

There are 0 best solutions below