I'm trying to get a dynamic service locator written in JavaScript using Harmony Proxies (Node.js). Basically you would create a new container:
var container = new Container();
You would then be able to set and get values like a traditional service locator:
container.set('FM', {});
container.get('FM');
container.get('FM', function(FM) {
});
You can even have namespaces that acts like sub-objects:
container.set('FM.Object', {});
The problem is with the dynamic aliases.
var App = container.alias('App');
When creating an alias, it creates a new proxy object where you can manipulate it like a traditional object, but it's an alias to get()
and set()
methods.
Instead of:
container.get('App.Hello');
With an alias, you would use:
App.Hello
The problem is with deep namespaces. Let's say you're trying to access App.Hello.World.Controller
, because it goes through the proxy one namespace at a time and not all at once (like that full namespace). How would I know if the user is calling App.Hello
to retrieve the value stored in the service locator, or if the user wants to continue accessing deeper namespaces? You can't (From what I've tried).
What other way could you accomplish this?
Having the syntax App.Hello.World
which would return either a proxy, if it's going to access a deeper namespace, or the value stored in the service locator.
var App = container.alias('App');
App.Controller.Home.Method.Index
// It would go through:
App -> Proxy
App.Controller -> Proxy or Value?
App.Controller.Home -> Proxy or Value?
App.Controller.Home.Method -> Proxy or Value?
App.Controller.Home.Method.Index -> Proxy or Value?
Right now, I've assembled a convention for dealing with the two options. You would use singular names for retrieving the value and plural for returning a proxy. This was just a quick "hack" as it's not very effective.
(If you need clarification or more info just let me know)