0

Code speaks a thousand words; is there a way to do what I'm attempting here?

Users "Root" Route

Loading common selectable options at the top level for use within sub-routes such as index, show, edit, create, etc...

-- /pods/users/route.js

model() {
    return RSVP.hash({
        userCategories: this.store.findAll('user-category'),
        userSources:    this.store.findAll('user-source'),
        userGroups:     this.store.findAll('user-group'),
    });
},

Users "Create" Route

With the sub-route of /create I want to loop through the already-loaded options above and load them into the template as named variables:

-- /pods/users/create/route.js

setupController(controller, model) {

    let rootModel = this.modelFor('users');

    rootModel.forEach(function (model, name) {
        set(controller, name, model);
    }); <-- THIS IS THE BROKEN PART...

    ...

}

The Problem

On the .forEach loop I am getting Error while processing route: rootModel.forEach is not a function TypeError: rootModel.forEach is not a function

Is there an "Ember Way" of looping through that root model, or will I be stuck with loading it in within a top level variable, ie;

-- /pods/users/create/route.js

setupController(controller, model) {

    let rootModel = this.modelFor('users');

    set(controller, 'rootModel', rootModel);

    ...

}
SamSebastien
  • 181
  • 1
  • 3
  • 9
  • Why would you need to loop through it? With the way you defined your model hook now, you can access the data in the template with i.e `{{#each model.userCategories as |category|}}` – Igor May 30 '16 at 07:17
  • I guess it's more a readability preference, I would prefer to be able to access `userCategories` without the leading `model`. Logically I know it doesn't really matter either way but to me `{{#each userCategories as |userCategory|}}` just seems cleaner. – SamSebastien Jun 04 '16 at 01:14

1 Answers1

4
Object.keys(rootModel).forEach(function(name) {
  var model = rootModel[keyName];
  set(controller, name, model);
  // ...
});

or possible you just need not RSVP.hash but RSVP.all which return a promise which resolves with array contains resolved values?

Vasiliy vvscode Vanchuk
  • 7,007
  • 2
  • 21
  • 44