How to automatically extend routes and controllers?


#1

Hi,

Is there any way to automatically extend routes and controller ? I want them to extend from GenericRoute and GenericController (which I have overload) and I want the routes and controllers which would be generated by Ember, to be extended from my Generic"Stuff"…

As I have the list of the route/controller names, my first attempt was to do the stuff in the App.initializer:

App.initializer({
    ...

    initialize: function(container, application) {
        for (var name in names) {
            var routeName = name+'DisplayRoute';
            App[routeName] = App.GenericDisplayRoute.extend();
        }
    }
});

In this example, it would generate <name>DisplayRoute routes, but I got an Uncaught #<error> when I reload my browser…

Any Ideas ?

Thanks !


#2

I’m not entirely sure this is the correct “Ember way”, but what about just reopen() the Ember.Route and Ember.Controller? Then anything created from them will get your modifications. Checkout the object Guide for a quick overview.


#3

If you’re using globals, you can define App.Controller and App.Route and ember will use those instead of Ember.Controller and Ember.Route. You’ll also need to implement App.ArrayController and App.ObjectController if you want to use them.


#4

Thank you, Panman and Joe for yours answers, but what I actually want to do is a little bit more complexe:

I have 4 generic routes:

  • App.GenericListRoute
  • App.GenericDisplayRoute
  • App.GenericNewRoute
  • App.GenericEditRoute

Each of them has a specific behavior. What I want is, for each names, generate the 4 routes. Example for BlogPost:

  • App.BlogPostListRoute
  • App.BlogPostDisplayRoute
  • App.BlogPostNewRoute
  • App.BlogPostEditRoute

So, reopening Ember.Route or creating App.Route is not enough…

Any idea of a way to implement this behavior ?


#5

I finally found what was wrong: in my first example, I created a route but didn’t register it in the router.

So, this is the working version:

App.initializer({
    ...

    initialize: function(container, application) {
        for (var name in names) {
            var routeName = name+'DisplayRoute';
            application.Router.map(function() {
                this.resource(underscoredType, function(){
                    this.route('display', {path: '/:id'});
                });
            });
            App[routeName] = App.GenericDisplayRoute.extend();
        }
    }
});