简体   繁体   中英

Emberjs: Loading data for other route

Resolved itself when updating to emberjs-rc.2. See answer.

In my application, I have 2 pages with wizard-ish functionality. On the first page, the user will provide some data, and on the next page the user will select an item in a list built based on the data on the first page. What I'm trying to do is to start loading the data for the second page as soon as the required data on the first page is valid, even if the user is still on the first page. This because building the list server will take some time, an I'd like to have the data ready when transitioning to the next page.

The two pages / routes are nested under the same resource.

My routes:

App.YayRoute = Ember.Route.extend({

  events: {
    dataValid: function() {
      this.controllerFor('yaySecond').send('loadStuff');
    }
  }
});

App.YaySecondRoute = Ember.Route.extend({
  events: {
    loadStuff: function() {
      this.controller.set('stuff', App.Stuff.find());
    }
  }
});

In YayFirstController, I'm observing relevant data and doing this.send('dataValid') when it is. The top route YayRoute picks this up ok, and triggers loadStuff on the second route. App.Stuff.find() looks like this

find: function() {
    var result = Ember.A([]);
    $.getJSON(url, function(data) {
        // populate result
    });
    return result;
}

Everything is being run when intended, but my problem is that stuff on YaySecondController is not populated when called from loadStuff. If I add controller.set('stuff', App.Stuff.find()) contents of loadStuff to setupController on YaySecondRoute, the data will load OK. But then I lose the coolness with the data being loaded as soon as possible. Any ideas?

So, this resolved itself when updating to emberjs-rc.2 today. I'm still new to ember, so I have no idea what was going on. Anyway, is this the correct way to do this? It seems slighty cumbersome.

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