2013-04-10 53 views

回答

4

目前沒有支持的方法。該App.Router.map呼叫用此代碼行235-247處理:https://github.com/emberjs/ember.js/blob/master/packages/ember-routing/lib/system/router.js

Ember.Router.reopenClass({ 
    map: function(callback) { 
     var router = this.router = new Router(); 

     var dsl = Ember.RouterDSL.map(function() { 
      this.resource('application', { path: "/" }, function() { 
      callback.call(this); 
      }) 
     }); 

     router.map(dsl.generate()); 
     return router; 
    } 

的地圖被覆蓋每次調用Router.map的回調上一次調用Router.map沒有持續的時間。

編輯 是好還是壞,我有一個拉請求改變行爲以允許多次調用App.Router.map。我們將看到會發生什麼。你可以在這裏https://github.com/emberjs/ember.js/pull/2485

遵循另一個編輯

我寫了一個要點做在用戶態就是我拉的要求做。這會讓你在運行時映射路由。只要您的來電與我定義

https://gist.github.com/grep-awesome/5406461

更改答案編輯

由於這種拉請求的方法添加此代碼,則更換App.Router.map,你現在可以調用map多次。 https://github.com/emberjs/ember.js/pull/2892

1

我看到wmarbut的答案沒有被接受,但它是一個很好的(對我來說)。看來他的補丁正在進入Ember版本,但在此之前,這是一些使用補丁的代碼。 (不要接受我的回答,我只是很高興能夠找到此答案。)我打算將它用作讓內容驅動導航的解決方案的一部分。好問題,user1517325和謝謝,wmarbut!

// was an all-in-one router map as Ember likes it 
    // App.Router.map(function() { 
    // this.resource("foods", function(){ 
    //  this.route("index", {path: "/"}); 
    // }); 
    // this.route("fourOhFour", { path: "*:"}); 
    // }); 

    //wmarbut's workaround until his patch is applied 
    App.map_routes = []; 

    App.MapRoutes = function(routes) { 
     App.map_routes.push(routes); 
     return App.Router.map(function() { 
     var route_lamda, _i, _len, _ref; 
     _ref = App.map_routes; 
     for (_i = 0, _len = _ref.length; _i < _len; _i++) { 
      route_lamda = _ref[_i]; 
      route_lamda.call(this); 
     } 
     return true; 
     }); 
    }; 

    //partial mapping 
    App.MapRoutes(function() { 
    this.resource("foods", function(){ 
    }); 
    }); 

    //some more mapping 
    App.MapRoutes(function() { 
    this.resource("foods", function(){ 
     this.route("index", {path: "/"}); 
    }); 
    }); 

    //even more mapping 
    App.MapRoutes(function() { 
    this.route("fourOhFour", { path: "*:"}); 
    }); 
1

在最新發布的ember.js RC7它被添加功能到Router.map允許它沒有地圖被覆蓋多次調用。這將允許在運行時添加路由。

希望它有幫助。