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

Bug with Injector: eager fail when singleton #11

Closed
ilann opened this issue May 14, 2012 · 2 comments
Closed

Bug with Injector: eager fail when singleton #11

ilann opened this issue May 14, 2012 · 2 comments

Comments

@ilann
Copy link

ilann commented May 14, 2012

using deftjs 0.6.4

browser : chrome 18 on windows 7.

was doing:

Deft.Injector.configure({
    preferences:  {
        className: 'MyApp.preferences.Preferences',
        eager: true
    },
    ...
});

created MyApp.preferences.Preferences as singleton

Ext.define("MyApp.preferences.Preferences",{

    singleton : true,

  ...

});

Got this in Chrome console:

ext-dev.js:7941Uncaught Error: [Ext.create] MyApp.preferences.Preferences' is a singleton and cannot be instantiated
@johnyanarella
Copy link
Member

Because you've used the singleton: true configuration when defining your class, the Sencha class system automatically creates a single instance of the class you defined in that file, and sets MyApp.preferences.Preferences with a static reference for that instance (rather than an instantiatable class definition). Consequently, Deft JS's internal attempt to eagerly create an instance of the specified class, i.e.Ext.create('MyApp.preferences.Preferences'), fails with that error.

If you remove that line, Deft JS will be able to eagerly create an instance of the MyApp.preferences.Preferences class and will fulfill any injection requests with a singleton instance managed by the IoC container.

You're not the first person to run into this - so I'm considering adding logic to detect classes that are explicitly defined to be singletons via the Sencha class system. Provided we can obtain a reference to the underlying class instance, we can sidestep this confusion and populate the IoC container with that same reference. In the case where a class has been defined as a singleton and the IoC container is asked to configure it as a prototype, we will still need to throw an error. However, it will be Deft JS throwing the error, rather than the Sencha class system (which threw the error you reported). We'll want to make that error explicitly guide the developer as to how to fix the problem.

@ghost ghost assigned johnyanarella May 14, 2012
@ilann
Copy link
Author

ilann commented May 14, 2012

Problem solved. Thank You. Great work!

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