blendsdk / blend-class-system

Easy OOP class-system for JavaScript
MIT License
4 stars 0 forks source link

Singleton not being instantiated #1

Closed alexandrubau closed 8 years ago

alexandrubau commented 8 years ago

I find your project very interesting, although I've ran into some problems. It seems that the 'singleton' attribute doesn't work, neither the 'statics'.

// index.js
require('blend-class-system');

App.Main.hello();
// App/Main.js

Blend.createClass('App.Main', function(){
    singleton: true,
    hello: function(){
        console.log('world');
    }
});
/Users/abau/Documents/Projects/loki/index.js:3
App.Main.hello();
^

ReferenceError: App is not defined
    at Object.<anonymous> (/Users/abau/Documents/Projects/loki/index.js:3:1)
    at Module._compile (module.js:425:26)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Function.Module.runMain (module.js:457:10)
    at startup (node.js:138:18)
    at node.js:974:3

Any help ?

Thanks

alexandrubau commented 8 years ago

Nevermind, it was my mistake. I needed to use the 'requires' attribute in order to have the App object.

blendsdk commented 8 years ago

The correct code would be:

// index.js
require('blend-class-system');

Blend.defineClass('App.Main',{
    singleton: true,
    hello: function(){
        console.log('world');
    }
});

App.Main.hello();
// App/Main.js
alexandrubau commented 8 years ago

I'm still having a little bit of trouble... The singleton classes don't get automatically instantiated unless they are required in someone's class or manually created using Blend.create(). Once instantiated, they are available without having to add them inside the requires array.

Is this the intended behaviour ?

Thank you very much @blendsdk .

blendsdk commented 8 years ago

Hi,

The singletons are only available when there are actually required by another class, the reason for this ia because we cannot auto-instantiate every singleton when the “program” starts. This is why you need to “require” them.

I hope it helps.

On 24 Nov 2015, at 23:29, Alexandru Bau notifications@github.com wrote:

I'm still having a little bit of trouble... The singleton classes don't get automatically instantiated unless they are 'required'. Once they are instantiated by any class, they are available without having to add them inside the requires array.

Is this the intended behaviour ?

Thank you very much @blendsdk https://github.com/blendsdk .

— Reply to this email directly or view it on GitHub https://github.com/blendsdk/blend-class-system/issues/1#issuecomment-159426531.

alexandrubau commented 8 years ago

I think it would be nice to be auto-instantiated. Because right now you need to add them in the first Blend class that you instantiate ( ex: App.Main ) although you won't use them inside that class. You put them there just so that they ( the singletons ) could get instantated and be used in the other classes.

Although in the documentation says

Singleton classes, which are classes that have only one instance and are _instantiated automatically_.

As a side note, I really like your framework. I've worked with ExtJS and find it very familiar. I think it has potential.

Thanks