Skip to content Skip to sidebar Skip to footer

Programmatically Create New Routes In Ember

I am using a json file pulled from the server to configure my website, and to tell each page what it's title is. The json file looks like this: [{'route': 'student', 'title': 'Stud

Solution 1:

I have used this to create the routes dynamically:

App = Ember.Application.create();
App.MenuController = Ember.ArrayController.create();

App.deferReadiness();

simulateAjax(function(data) {
    App.MenuController.set("content", data);    
    App.Router.map(function() {
        var router = this;
        data.forEach(function(item) {
           router.route(item.route); 
        });
    });
    App.advanceReadiness();
});

simulateAjax is just a function that simulate an ajax call to the server.

App.deferReadiness(); and App.advanceReadiness(); delay the application startup, so you don't have the effect of the screen blink, because of the update of new added routes. Since our application ready is: when the routes are created, not document ready.

And here is a demo

UPDATE

link-to helper support dynamic routing, using a object and path. But at the moment its needed to use ENV.HELPER_PARAM_LOOKUPS = true.

With this, we don't need to create a custom linkTo to handle dynamic path, like the first demo ;)

New demo

Post a Comment for "Programmatically Create New Routes In Ember"