Programmatically create new routes in Ember

前端 未结 1 1652
不知归路
不知归路 2021-02-08 19:37

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\": \"s         


        
1条回答
  •  逝去的感伤
    2021-02-08 20:28

    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

    0 讨论(0)
提交回复
热议问题