tcorral / Hydra.js

Create a scalable, maintainable and module oriented system using Hydra.js
http://tcorral.github.com/Hydra.js
MIT License
102 stars 12 forks source link

API Suggestion #33

Open gfreeau opened 10 years ago

gfreeau commented 10 years ago

I want to understand why the API is just the Hydra object. What is the reason for extending the Hydra object instead of having a separate object just for the common API?

If I have this code:

Hydra.extend("hello", { say: function() { console.log('hello'); }});

Then in my module I could do this:

Hydra.module.register('adslot', ['$api'], function (api) {
        return {
            init: function () {
                api.hello.say();
            }
        };
    });

This is very intuitive, however I still do not see why "api" has to be the Hydra object instead of being it's own object or object literal. Bus is a separate object (Hydra.bus), why not API (Hydra.api)?

Then we could extend Hydra.api instead of just Hydra, I think this is better separation. The API is separate from Hydra.

In function dependencyinjection, then it would read:

oMapping = {
    '$bus': Bus,
    '$module': Hydra.module,
    '$log': ErrorHandler,
    '$api': Hydra.api,
    '$global': root,
    '$doc': root.document || null
},

As it is now, with my newly added 'hello' API property, these are the properties on the Hydra object:

This would be much cleaner with an "api" property and then all API calls extend that. e.g Hydra.api.hello.say().

There is no difference inside module because of dependency injection, but it makes the code much easier to read and understand if you want to contribute to Hydra.js

tcorral commented 10 years ago

I'll consider it for the next release.

You're welcome