5

在Ember 1.0-rc2 附带的新版本中,是否可以在运行时添加路由? Ember.Router

4

3 回答 3

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

另一个编辑

我已经写了一个要点来完成我的拉取请求在用户空间中所做的事情。这将允许您在运行时映射路线。只需添加此代码,然后将您的调用替换为App.Router.map我定义的方法

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

答案更改编辑

从这个拉取请求开始,您现在可以map多次调用。https://github.com/emberjs/ember.js/pull/2892

于 2013-04-16T21:00:03.780 回答
1

I see that wmarbut's answer hasn't been accepted, but it is a good one (for me). It seems that his patch is on its way into the Ember release, but until then this is some code that uses his patch. (Don't accept my answer, I'm just happy to have found this.) I'm planning to use it as part of a solution to let content drive navigation. Good question, user1517325 and thanks, 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: "*:"});
  });
于 2013-04-27T16:11:53.460 回答
1

在最新版本的 ember.js rc7 中,它被添加Router.map到允许多次调用它而不覆盖地图的功能。这将允许在运行时添加路由。

希望能帮助到你。

于 2013-08-15T21:04:59.453 回答