Ember JS: How to load a second model based on data from a first

JamesG

I have an Ember app that, rather than using Ember Data, uses our own custom AJAX data layer to talk to an API.

We're able to load two models at once using RSVP - one is a Project object via our API wrapper, the second is an object representing the logged in user. Both are passed into the controller and templates and work just fine.

But I have a need to load a second model, based on a value in the returned Project object.

Once I've loaded a model in my route like this...

App.ProjectUpdateRoute = Ember.Route.extend({

    setupController: function(controller, model) {
        controller.set('model', model);
    },

    model: function(params) {

        return Ember.RSVP.hash({
            // Load Project from API - /myapi/v1/Project/:ProjectID
            Project : App.Project.create().findById(params.ProjectID),
            // Load current user from local object
            User : App.AuthUser,
        });
    },
});

...I have a Project object (or rather model.Project) with various properties including the ID of the User that owns the project.

Now I'd like to make a second API call to /myapi/v1/User/:UserID to get the User's details.

Everything I've tried - including adding further App.User.create().findById(UserID) calls into the route's setupController function and the controller - results in the correct API call, but it's asyncronous, so Ember carries on rendering the page and I can't show the result of the API call on the page.

So - how, and where in the Ember structure, do I load a second model based on data from the first? How can I get ember to wait for the resolved promise of this second AJAX call?

UPDATE

I've also tried using afterModel:function() which is almost what I need - it makes the second API call in the right place in the app flow, but I still need to add the result into my existing model array:

afterModel: function(model, tranistion, params) {

    // Returns the promise but doesn't update 'model'
    return Ember.RSVP.hash({
        ProjectOwner : App.User.create().findById(model.Project.UserID)
    });
}
Kingpin2k

Chain the promise, and Ember will take the final resultant (from the model hook)

model: function(params) {

  return Ember.RSVP.hash({
      // Load Project from API - /myapi/v1/Project/:ProjectID
      Project : App.Project.create().findById(params.ProjectID),
      // Load current user from local object
      User : App.AuthUser,
    }).then(function(results){
      return App.User.create().findById(results.Project.UserID).then(function(user){
        results.projectUser = user;
        return results;
      });
    });
},

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ember JS: How to load a second model based on data from a first

From Dev

How can I do an Ember.js computed property on an Ember Data model based on an async relationship?

From Dev

How can I do an Ember.js computed property on an Ember Data model based on an async relationship?

From Dev

Ember js filtering the the data from the model

From Dev

How to publish a second collection based on data in first

From Dev

Ember.js - Call parent model hook to load data for child

From Dev

Ember.js - Call parent model hook to load data for child

From Dev

Load data belonging to a parent model with Ember.js

From Dev

Partial Load from Ember Model

From Dev

How can I extract row from a second dataframe based on data-pairs from first dataframe?

From Dev

How to set the tagName of an Ember component based data in the model?

From Dev

How to set the tagName of an Ember component based data in the model?

From Dev

Ember.js - Ember Data not returning a model

From Dev

Populating Ember JS handlebars template with Ajax data from model?

From Dev

Populating Ember JS handlebars template with Ajax data from model?

From Dev

get data from my model to my controller (Ember.js)

From Dev

How to load Second View from First View with Xamarin/iOS

From Dev

Angular 2: How to load data from service before creating model based formgroup

From Dev

how to set the data in second component based on first component selection in UIPickerview

From Dev

Ember.js load model for multiple outlets

From Dev

Ember.js load model for multiple outlets

From Dev

How to set model data in an Ember.js unit test for a controller

From Dev

return first model ember js template

From Dev

How to sort many to many through a join model data with Ember.js and Ember Data

From Dev

How to correctly access Ember model data from controller

From Dev

How do I create a promise in Ember.js for an Ember-data model

From Dev

How to tell ember.js and ember-data version from ember-CLI?

From Dev

ember-data: How to tell if a model's async: true relationship is loaded without triggering a load?

From Dev

How to continue even if Ember.js model hook doesn't load all promises?

Related Related

  1. 1

    Ember JS: How to load a second model based on data from a first

  2. 2

    How can I do an Ember.js computed property on an Ember Data model based on an async relationship?

  3. 3

    How can I do an Ember.js computed property on an Ember Data model based on an async relationship?

  4. 4

    Ember js filtering the the data from the model

  5. 5

    How to publish a second collection based on data in first

  6. 6

    Ember.js - Call parent model hook to load data for child

  7. 7

    Ember.js - Call parent model hook to load data for child

  8. 8

    Load data belonging to a parent model with Ember.js

  9. 9

    Partial Load from Ember Model

  10. 10

    How can I extract row from a second dataframe based on data-pairs from first dataframe?

  11. 11

    How to set the tagName of an Ember component based data in the model?

  12. 12

    How to set the tagName of an Ember component based data in the model?

  13. 13

    Ember.js - Ember Data not returning a model

  14. 14

    Populating Ember JS handlebars template with Ajax data from model?

  15. 15

    Populating Ember JS handlebars template with Ajax data from model?

  16. 16

    get data from my model to my controller (Ember.js)

  17. 17

    How to load Second View from First View with Xamarin/iOS

  18. 18

    Angular 2: How to load data from service before creating model based formgroup

  19. 19

    how to set the data in second component based on first component selection in UIPickerview

  20. 20

    Ember.js load model for multiple outlets

  21. 21

    Ember.js load model for multiple outlets

  22. 22

    How to set model data in an Ember.js unit test for a controller

  23. 23

    return first model ember js template

  24. 24

    How to sort many to many through a join model data with Ember.js and Ember Data

  25. 25

    How to correctly access Ember model data from controller

  26. 26

    How do I create a promise in Ember.js for an Ember-data model

  27. 27

    How to tell ember.js and ember-data version from ember-CLI?

  28. 28

    ember-data: How to tell if a model's async: true relationship is loaded without triggering a load?

  29. 29

    How to continue even if Ember.js model hook doesn't load all promises?

HotTag

Archive