How can I have multiple ajax calls in one route?

Pedro

I'm trying to get two get 2 lists of blog posts from 2 categories (news and events), then display them in 2 different columns of my home page. It is required for me to perform 2 separate Ajax calls to get those blog posts. I do not use ember-data for this operation, as I don't see the advantage of using it in this scenario (but I may be wrong).

export default Ember.Route.extend({
  setupController(controller, model) {
    var wpUrl = 'http://public-api.wordpress.com/rest/v1/sites/company.wordpress.com/posts/?number=2&category=';

    Ember.$.ajax({ url: wpUrl + 'news', dataType: "jsonp", type: 'GET' }).then(function (data) {
      controller.set('news', data.posts);
    });
    Ember.$.ajax({ url: wpUrl + 'events', dataType: "jsonp", type: 'GET' }).then(function (data) {
      controller.set('events', data.posts);
    });
  }
});

The above code works. But from what I read in the Ember documetation, I should be getting those data in the model hook (instead of setupController) to take advantage of promises. So I tried to re-write my code this way:

export default Ember.Route.extend({
  model() {
    var wpUrl = 'http://public-api.wordpress.com/rest/v1/sites/company.wordpress.com/posts/?number=2&category=';

    return {
      news: function () {
        return Ember.$.ajax({ url: wpUrl + 'news', dataType: "jsonp", type: 'GET' }).then(function (data) {
          return data.posts;
        })
      },
      events: function () {
        return Ember.$.ajax({ url: wpUrl + 'events', dataType: "jsonp", type: 'GET' }).then(function (data) {
          return data.posts;
        })
      }
    };
  }
});

But this does not work. The Ajax calls are done but too late, after the page has rendered. I'm not sure here what I'm doing wrong. Would there be any advantage of using ember-data for that scenario?

Willem de Wit

You can return an hash of Promises with RSVP.hash()

You could do this:

export default Ember.Route.extend({
    model() {
        var wpUrl = 'http://public-api.wordpress.com/rest/v1/sites/company.wordpress.com/posts/?number=2&category=';

        return new Ember.RSVP.hash({
            news: Ember.$.ajax({ url: wpUrl + 'news', dataType: "jsonp", type: 'GET' }),
            events: Ember.$.ajax({ url: wpUrl + 'events', dataType: "jsonp", type: 'GET' })
        });
    }
});

Read more about promises in Ember here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I have multiple instances of one controller for multiple templates

From Dev

How can I have one submit button for multiple forms?

From Dev

How can I have multiple Collection Views in one View Controller?

From Dev

How can I have one Kubernetes LoadBalancer balance to multiple services?

From Dev

How can I have multiple passphrases for unlocking one encrypted partition?

From Dev

How can I have multiple Collection Views in one View Controller?

From Dev

Can I use one route for multiple functions?

From Dev

How can I call a function after all ajax calls have loaded?

From Dev

Without jQuery, how can javascript check that multiple ajax calls have been completed, and once they have, execute a piece of code?

From Dev

Multiple Ajax Calls, One Callback

From Dev

Multiple Ajax Calls, One Callback

From Dev

How can I make AJAX calls to multiple URLs based on button clicks?

From Java

How can I chain multiple calls to `into()`?

From Dev

How to modify ajax calls or promises chain so that multiple variables (from an array) can be used in separate ajax calls?

From Dev

How can I have one unique button in multiple components of a layout in android?

From Dev

How can I run multiple commands which have & in one command line?

From Dev

How can I run multiple commands which have & in one command line?

From Dev

How can I have one unique button in multiple components of a layout in android?

From Dev

How can I have multiple terminal sessions through one single SSH connection?

From Dev

How can I have a stacked plot with a shared X axis and multiple Y axis on one of the plots?

From Dev

How can I have multiple controller files?

From Dev

How can I have multiple localhosts in a machine?

From Dev

How can i have multiple mappers and reducers?

From Dev

How can i have multiple Navbars in a page

From Dev

How can I know when Ajax calls are finished with getScript?

From Dev

How can I use an ajax request to fire Webservice calls in Javascript

From Dev

How can I make many jQuery ajax calls look pretty?

From Dev

How can I know when Ajax calls are finished with getScript?

From Dev

How to avoid multiple AJAX calls?

Related Related

  1. 1

    How can I have multiple instances of one controller for multiple templates

  2. 2

    How can I have one submit button for multiple forms?

  3. 3

    How can I have multiple Collection Views in one View Controller?

  4. 4

    How can I have one Kubernetes LoadBalancer balance to multiple services?

  5. 5

    How can I have multiple passphrases for unlocking one encrypted partition?

  6. 6

    How can I have multiple Collection Views in one View Controller?

  7. 7

    Can I use one route for multiple functions?

  8. 8

    How can I call a function after all ajax calls have loaded?

  9. 9

    Without jQuery, how can javascript check that multiple ajax calls have been completed, and once they have, execute a piece of code?

  10. 10

    Multiple Ajax Calls, One Callback

  11. 11

    Multiple Ajax Calls, One Callback

  12. 12

    How can I make AJAX calls to multiple URLs based on button clicks?

  13. 13

    How can I chain multiple calls to `into()`?

  14. 14

    How to modify ajax calls or promises chain so that multiple variables (from an array) can be used in separate ajax calls?

  15. 15

    How can I have one unique button in multiple components of a layout in android?

  16. 16

    How can I run multiple commands which have & in one command line?

  17. 17

    How can I run multiple commands which have & in one command line?

  18. 18

    How can I have one unique button in multiple components of a layout in android?

  19. 19

    How can I have multiple terminal sessions through one single SSH connection?

  20. 20

    How can I have a stacked plot with a shared X axis and multiple Y axis on one of the plots?

  21. 21

    How can I have multiple controller files?

  22. 22

    How can I have multiple localhosts in a machine?

  23. 23

    How can i have multiple mappers and reducers?

  24. 24

    How can i have multiple Navbars in a page

  25. 25

    How can I know when Ajax calls are finished with getScript?

  26. 26

    How can I use an ajax request to fire Webservice calls in Javascript

  27. 27

    How can I make many jQuery ajax calls look pretty?

  28. 28

    How can I know when Ajax calls are finished with getScript?

  29. 29

    How to avoid multiple AJAX calls?

HotTag

Archive