简体   繁体   中英

Looping through an Ember JS RSVP hash?

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

    ...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM