Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get a reference to the app instance? #21

Closed
neilmcguigan opened this issue Jun 8, 2012 · 2 comments
Closed

How to get a reference to the app instance? #21

neilmcguigan opened this issue Jun 8, 2012 · 2 comments

Comments

@neilmcguigan
Copy link

In Sencha's controllers, the application is passed in, and set to controller.application.

How do I get a reference to the application from a Deft ViewController? Or do I not want to do that?

How do I fire an app-wide event?

Thanks

Neil

@johnyanarella
Copy link
Member

The answer depends on whether you are planning on using any of the other capabilities of Ext.app.Application. If you are using the Deft JS IoC container for dependency injection (instead of using the stores, views, models configs in Ext.app.Application) and Deft JS View Controllers (instead of Ext.app.Controller and the controllers config in Ext.app.Application), chances are you don't not need Ext.app.Application anymore.

If you want an application-wide event bus, you can configure the IoC container with one and inject it as needed. To do so, you simply configure the IoC container with an instance of Ext.util.Observable to be injected wherever needed.

Deft.Injector.configure({
    eventBus: 'Ext.util.Observable'
});

(This works because in addition to being usable as a mixin, Ext.util.Observable can also be directly instantiated and used as a standalone event bus.)

If it turns out you still need Ext.app.Application, you can configure the IoC container with the Ext.app.Application instance as the value, like so:

Ext.application({
    name: 'MyApp',
    launch: function() {
        Deft.Injector.configure({
            'application': {
                value: this
            }
        });

        // Create Viewport and views
        ...
    }
});

I created an example of this approach on JSFiddle here: http://jsfiddle.net/CodeCatalyst/K2ZgR/

Deft.Injector.configure() can be called multiple times in your application. Each time it is called it adds or updates the specified identifiers. In this case, you'll want to do the majority of your IoC container configuration in Ext.onReady() and only configure the application reference within your Ext.app.Application's launch() method.

Be careful, though. If you use Ext.app.Application's stores or controllers configs, the singleton instances it creates are instantiated before the Ext.app.Application instance's constructor or launch() method are fired. If you've instantiated classes that depend on the application before then, Deft.Injector will throw an error because the application identifier can only be resolved after its been created and you've configured the IoC container with that reference.

@neilmcguigan
Copy link
Author

That's awesome John. Thank you a ton.

Deft is a much better solution than Sencha's first attempt at MVC.

Cheers,

Neil

On Tue, Jun 12, 2012 at 8:42 AM, John Yanarella <
reply@reply.github.com

wrote:

The answer depends on whether you are planning on using any of the other
capabilities of Ext.app.Application. If you are using the Deft JS IoC
container for dependency injection (instead of using the stores, views,
models configs in Ext.app.Application) and Deft JS View Controllers
(instead of Ext.app.Controller and the controllers config in
Ext.app.Application), chances are you don't not need
Ext.app.Application anymore.

If you want an application-wide event bus, you can configure the IoC
container with one and inject it as needed. To do so, you simply configure
the IoC container with an instance of Ext.util.Observable to be injected
wherever needed.

Deft.Injector.configure({
   eventBus: 'Ext.util.Observable'
});

(This works because in addition to being usable as a mixin,
Ext.util.Observable can also be directly instantiated and used as a
standalone event bus.)

If it turns out you still need Ext.app.Application, you can configure
the IoC container with the Ext.app.Application instance as the value,
like so:

Ext.application({
   name: 'MyApp',
   launch: function() {
       Deft.Injector.configure({
           'application': {
               value: this
           }
       });

       // Create Viewport and views
       ...
   }
});

I created an example of this approach on JSFiddle here:
http://jsfiddle.net/CodeCatalyst/K2ZgR/

Deft.Injector.configure() can be called multiple times in your
application. Each time it is called it adds or updates the specified
identifiers. In this case, you'll want to do the majority of your IoC
container configuration in Ext.onReady() and only configure the
application reference within your Ext.app.Application's launch() method.

Be careful, though. If you use Ext.app.Application's stores or
controllers configs, the singleton instances it creates are instantiated
before the Ext.app.Application instance's constructor or launch()
method are fired. If you've instantiated classes that depend on the
application before then, Deft.Injector will throw an error because the
application identifier can only be resolved after its been created and
you've configured the IoC container with that reference.


Reply to this email directly or view it on GitHub:
#21 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants