Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember-Router: How to add a route in run-time in Ember 1.0-rc2?

In the new Ember.Router that shipts with Ember 1.0-rc2, is it possible add route in run-time?

like image 713
user1517325 Avatar asked Apr 10 '13 09:04

user1517325


2 Answers

There is not a supported method of doing this currently. The App.Router.map call is handled 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 map is overwritten every time you call Router.map as the callback for the previous call to Router.map is not persisted.

Edit For better or worse, I've got a pull request in to alter the behavior to allow multiple calls to App.Router.map. We'll see what happens. You can follow here https://github.com/emberjs/ember.js/pull/2485

Another Edit

I've written a gist to do what my pull request does in userland. This will let you map routes at runtime. Just add this code and then replace your calls to App.Router.map with the method that I've defined

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

Answer Changing Edit

As of this pull request, you may now call map multiple times. https://github.com/emberjs/ember.js/pull/2892

like image 146
wmarbut Avatar answered Sep 19 '22 23:09

wmarbut


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: "*:"});
  });
like image 42
laurelnaiad Avatar answered Sep 21 '22 23:09

laurelnaiad