2013-04-10 59 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允许它没有地图被覆盖多次调用。这将允许在运行时添加路由。

希望它有帮助。

相关问题