How can i set the ApplicationController in Ember.Router
Asked Answered
M

1

5

I there any other possibility to handle with Ember.Router controllers and views? My app structure generally depends on require.js which takes care of the corresponding dependencies.

In my example i'm doing the App.ApplicationController thing as the main router controller.

Here's a jsfiddle: http://jsfiddle.net/mediastuttgart/uMKGt/1/

But is there any chance to set this manually? I've found this commit message https://github.com/emberjs/ember.js/commit/be69395f5eec4187b1df052d7386bcda45f79475 where i can see, how to set the controller and view manually (anyway couldn't get this working). But couldn't find any example like:

App = Ember.Application.create();

App.MyController = Ember.Controller.extend({});
App.MyView = Ember.View.extend({});

Router = Ember.Router.extend({
    applicationController : App.MyController,
    root : Ember.Route.extend({
        index : Ember.Route.extend({
            route : '/',
            connectOutlets : function (router) {
                router.get('applicationController').connectOutlet(App.MyView);
            }
        })
    }),
    ...
});

EDIT

I'm trying to explain this a little bit more in detail. While we use require.js to manage or dependencies, we have multiple controllers which are responsible for their own parts of the entire application. Let's say we have a newsletter signup form. The controller used for this scenario is structured in

- app
-- controller
--- newsletter
---- signup.js
--- cart
--- checkout
-- view
-- model
-- router
-- template

As we use a custom function to generate namespaces Em.Provide('App.Controller.Newsletter'); which creates an object at App.Controller.Newsletter. This object now serves as controller base for our entire newsletter application. Beginning with App.Controller.Newsletter.Signup = Ember.Controller.extend({}); we need to pass in this instance as the "ApplicationController" for the router. As you can see, we can't just use App.ApplicationController to instantiate a controller for Ember.Router.

Moorefield answered 25/6, 2012 at 15:47 Comment(0)
O
9

Don't know anything about requirejs, but I think the only thing is to define an

App.ApplicationController = Ember.Controller.extend({...behavior here...})

and then, when calling

App.initialize()

The framework will then inject automatically this controller in the router.

Okelley answered 25/6, 2012 at 18:55 Comment(3)
N.B. The naming is important here. Ember looks for an ApplicationController under your application namespace. if the original question had used that instead of MyApplicationController it would have worked automatically.Opportuna
Thanks for the responses. I've updated my question with more details.Moorefield
@MichaelAlexanderFreund Sorry, my knowledge stops at this point. Perhaps by searching in github.com/emberjs/ember.js/blob/master/packages/… and github.com/emberjs/ember.js/blob/master/packages/ember-routing/… you'll find how to inject and use a custom controller as the main app controller...Okelley

© 2022 - 2024 — McMap. All rights reserved.