Skip to content

Singleton View & Controller? #1101

Closed
Closed

Description

@Onkelborg

I'm creating a single page application and I've decided on Ember.js. I'm investigating the new router, however, I think there are a bad design decision; singletons.

As far as I can tell, the idea is that the View is bound (data) to the Controller and events (actions) to the Router.

application.initialize(router) will create a singleton instance of each controller, and that singleton will be used whenever the controller is needed.

My problems:

  1. I'm planning a big application, and I want it easy to maintain. Requirejs will be heavily used. Currently, this is my connectOutlets:

    connectOutlets: function (router) {
                    require(["app/trapeze/user"], function () {
                        router.set("loginController", app.LoginController.create());
                        router.get("loginController").setProperties({
                            target: router,
                            controllers: router,
                            namespace: app
                        });
                        router.get("applicationController").connectOutlet("login");
                    });
                }

    Since the loginController singleton is created way earlier (somewhere between 1 ms and 10 years :) ) in app.initialize() I have to manually create it myself, and that doesn't feel good.. :)

  2. What about nesting? In another Route I've tried this:

    router.get("loginController").connectOutlet("login");

    I didn't expect it to work, and it didn't :)

    Uncaught RangeError: Maximum call stack size exceeded
    beforeKey
    xformBefore
    invokeAction
    iterateSet
    sendEvent
    notifyObservers
    Ember.notifyBeforeObservers
    propertyWillChange
    Ember.View.Ember.Object.extend._elementWillChange
    Ember.View.Ember.Object.extend.forEachChildView
    Ember.View.Ember.Object.extend._elementWillChange
    and then it repeats

I like the movement away from singletons, but this is a problem (it's a problem that there are singletons, not the idea of moving away from singletons), any ideas or comments?

Activity

  1. trek commented on Jul 3, 2012

    @trek
    Member

    I'm confused about why you'd want to nest a login view within itself... There's a "yo dawg I heard" joke in here somewhere.

    If you don't have all your controllers available, you will indeed need to add it yourself there. We could wrap that up in a a router.addController method to have a standard way to add them later.

  2. Onkelborg commented on Jul 3, 2012

    @Onkelborg
    Author

    I don't want a login view inside itself, it was just a test. A more practical example would have been a project manager, for example, where a Task could have other Tasks. In that case you would end up with a task inside a task inside a task.. :)

    (But then I suspect the router has to be rewritten a bit more to allow "on-the-fly" routes as the length of the chain isn't known until runtime)

    edit:

    My point is that I think it's a bad idea to store the instances in the router, I think they are more related to the route. And I don't think they should be store with the same, but intial lower case, name as their constructor - consider a diff-tool with two windows for comparing two files side-by-side. I'm thinking somewhere along the path of storing the controller attached, somehow, to the outlet it's rendered in?

    I do think this is necessary to address, it's a problem, and it's a lot easier to fix now without having to care about backwards compatibility

  3. wagenet commented on Jul 4, 2012

    @wagenet
    Member

    @wycats, @tomdale You made a lot of the decisions here. It would be great if you could chime in.

  4. ghost assigned on Jul 9, 2012
  5. stas commented on Oct 15, 2012

    @stas

    @Onkelborg you should use connectOutlet and set the context to what view and controller you want to use.
    It has nothing to do with singleton or not.

    Also take a look at source code of the connectOutlet:
    https://github.com/emberjs/ember.js/blob/master/packages/ember-views/lib/system/controller.js#L133

  6. wagenet commented on Oct 19, 2012

    @wagenet
    Member

    I don't think we're going to change anything here. @Onkelborg hopefully @stas's suggestion was useful.

  7. stas commented on Oct 19, 2012

    @stas

    Oh, one important thing, in order to leverage from existing routes, don't forget to initialize the target attribute with current router instance.

    require( [ 'app/view/myview', 'app/controller/mycontroller' ],
      function( MyView, MyController ) {
        router.get( 'applicationController' ).connectOutlet( {
          viewClass: MyView,
          controller: MyController.create({ target: router }),
          context: 'ifAny'
        } );
      }
    ); 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions