Hi Guyz,
I got a typical cross dependency case that I solved, but I don't like my
solution, as it's not that much elegant.
The situation is the following:
I have ElementA,and ElementB, that are "classes" (that can be instantiated).
ElementA instance holds the ID of a ElementB object, and ElementB holds a
list of ElementA IDs.
It's like ElementB is a parent of ElementA objects, and an ElementA object
can have only one parent.
I have ElementAProvider and ElementBProvider, that are objects that can
retrieve ElementA and ElementB data from a RESTful service.
These service returns instances of class ElementA and ElementB.
To make my objects easier to use, I added getParent() to ElementAprototype, and
getChildren() to ElementB prototype, so they returns instances of the
parent, or the children, instead of just the ID. These methods use
ElementAProvider et ElementBProvider to get these instances.
ElementA.prototype = {
getParent : function ()
{
return ElementBProvider.get( this.parentId );
}
}
ElementB.prototype = {
getChildren : function ()
{
return ElementAProvider.getSeveral( this.childrenIds );
}
}
The problem is, that when I add my factories to my module, I have to define
my dependencies :
module.factory( 'ElementA' , [ '$http', '$q', 'ElementBProvider',
factoryElementA ] );
module.factory( 'ElementB' , [ '$http', '$q', 'ElementAProvider',
factoryElementB ] );
module.factory( 'ElementAProvider', [ '$http', '$q', 'ElementA' ,
factoryElementAProvider ] );
module.factory( 'ElementBProvider', [ '$http', '$q', 'ElementB' ,
factoryElementBProvider ] );
So ElementA needs ElementBProvider, that needs ElementB, that needs
ElementAProvider, that needs ElementA.
To solve that, I use $injector to manually get dependencies just before
using them.
function factoryElementA ( $http, $q, $injector )
{
var ElementBProvider = null;
function _getDependencies ()
{
if ( ElementBProvider == null ) $injector.get( 'ElementBProvider' );
}
function ElementA ( /* … */ )
{
// …
}
ElementA.prototype = {
getParent : function ()
{
_getDependencies();
return ElementBProvider.get( this.parentId );
}
}
}
module.factory( 'ElementA', [ '$http', '$q', '$injector', factoryElementA ]
);
It works, but each time I need to use my dependencies, I have to check them.
Is there another smarter way to do it ?
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.