-
-
Notifications
You must be signed in to change notification settings - Fork 405
Closed
Description
Managing data in the route has been brought into question a few times, e.g. https://gist.github.com/machty/38bdc3057be92eadbb2e81efc59c4aac, #283
There is also an addon that attempts to accomplish some of the ideas below https://github.com/nickiaconis/ember-prefetch
I'd like to get more people thinking about this, since a gist gets lost pretty quickly.
The goal is to get some concrete ideas that could become an RFC and/or an addon.
What it should do
- Only block child routes based on some value, like a
blockChildRouteshook (kind of likebeforeModel) modelForthat returns a promiseproxy (resolved if parent blocked).modelthat returns an object and sets it on the controller (new defaultsetupController)resetControllerresets the keys set frommodelon the controller by default- Remove default
modelbehaviour (no auto id lookup via type_key syntax in router definition) - Something else?
Example Code
This code is just an idea to get you thinking.
import Route from 'some-new-route-addon';
export default Route.extend({
async data({ userId }) {
// the 'profile' route isn't a parent, so we must pass the arguments
// so that route gets the right data for us.
// no arguments and nested means that the existing arguments from the router are used and it might have already been loaded.
let { profile } = await this.dataFor('profile', { profileId: userId });
return {
profile,
user: this.store.findRecord('user', userId)
};
}
});
// controller.user === user
// controller.profile === profileImplementation
Sample implementation on top of existing Route class
import Route from '@ember/routing/route';
import { hash } from 'rsvp';
export default Route.extend({
model() {
if (typeof this.data === 'function') {
let result = this.data.call(this, ...arguments);
return hash(result);
}
},
setupController(controller, resolved) {
if (resolved) {
let dataKeys = Object.keys(resolved);
this.dataKeys = dataKeys;
controller.setProperties(resolved);
}
},
resetController(controller) {
if (this.dataKeys) {
let reset = this.dataKeys.reduce((obj, key) => {
obj[key] = undefined;
return obj;
}, {});
controller.setProperties(reset);
this.dataKeys = undefined;
}
}
});Relavent code
- Continue to the next route (not sure where this is called)
https://github.com/tildeio/router.js/blob/682e63d7814de7d9aef47dee364da76900ae4877/lib/router/transition-state.js#L69 - Call the model hooks (called from above)
https://github.com/tildeio/router.js/blob/master/lib/router/handler-info.js#L71
Docs
- router.js readme https://github.com/tildeio/router.js
- router.js architecture https://github.com/tildeio/router.js/blob/master/ARCHITECTURE.md
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels