Ember-router: how to add a route at runtime in Ember 1.0-rc2?

In the new Ember.Routerone that ships with Ember 1.0-rc2, is it possible to add a route at runtime?

+5
source share
3 answers

There is currently no supported method. The call is App.Router.maphandled by lines 235-247 of this code: 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;
    }

The card is overwritten every time you call Router.map, since the callback for the previous call is Router.mapnot saved.

Edit , pull, , App.Router.map. , . https://github.com/emberjs/ember.js/pull/2485

, , userland. . , App.Router.map ,

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

map . https://github.com/emberjs/ember.js/pull/2892

+4

, 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 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

In the latest version of ember.js rc7 , functionality was added Router.mapto allow it to be called several times without a rewritable card. This will add routes at runtime.

Hope this helps.

+1
source

All Articles