简体   繁体   中英

Ember.js - Setting a model and routing dynamically via API data

So I'm working on building a dynamic model for a project that reacts to data sent from an API. The api will return, among other things, what your location should be and this in turn becomes the url. So, eg:

 {
  location: 'xyz'
  (...)
 }

So currently my router will transition to the right route dynamically. But I still have to hardcode each route ( IndexRoute , LocationXYZRoute , LocationABCRoute , etc).

My goal is to create a single route that handles things dynamically. We'll call it App.LocationRoute and my routes would look something like:

App.Router.map(function() {
 this.resource(':location', function() {
   this.route(':subLocation')
  }
}

Now, I have two architectural questions:

1) Whats a good way to handle this sort of dynamic routing? (I've read through the guide about dynamic routing using the ':post_id' type example, but I think I need a more holistic example to really grasp it.

2) The API sends back a whole host of other data as well. I want to add this to the route's model but I also have some other static models. Doing...

this.controllerFor(location).set('content', APIdata);

... works, but it does not set for routes currently using static models. I tried something like:

this.controllerFor(location).set('apiData', APIdata);

and...

this.controllerFor(location).set('model:apiData', APIdata);

... but neither worked.

Any suggestions?

1) Yes, you should use dynamic segment

this.resource('location', { path: '/location/:location_id' }, function() {
    this.resource('sublocation', { path: '/sublocation/:location_id' });
});

2) Are you using ember-data? You could check sideloaded data. Anyway, you could read the json and set the payload of each entity for each specific route.

this.controllerFor('location').set('content', APIdata.location);
this.controllerFor('user').set('content', APIdata.user);

People could help you better, if you separate your questions and create a http://emberjs.jsbin.com/ with isolated each specific case?

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