Hey everyone

I have a 'problem', i just don't know what kind of problem i have. Maybe it 
is my understanding of deferred functions, or maybe it is my understanding 
of object prototypes, or maybe it is something completely else.
Anyway, here goes:

I have a Product object, and a Distributor object, and both are gotten via 
AJAX.
The Product has a DistributorId, and i'd like to display the Distributor 
name in a list (via ng-repeat).

Now what's completely clear to me is how i display static, simple objects 
in angular, but i never found anything on how to link objects to each other.
So i wrote this code for the Product prototype:

app.factory('Product', ['distributorsManager', function 
(distributorsManager) {
    function Product(productData) {
        if (productData) {
            this.setData(productData);
            this.getDistributor();
        }
    };

    Product.prototype = {
        setData: function (productData) {
            angular.extend(this, productData);
        },
        getDistributor: function(){
            //will be assigned to the current product
            var product;
            //set the scope to the current product
            //can't use 'this' later, because the scope might be on another 
object when the deferred returns
            //that's why we use a closure here
            (function(currentScope){
                product = currentScope;
            })(this);
            //go get the distributor, once you're done, assign the 
distributor to the product
            
distributorsManager.getDistributor(this.distributorId).then(function(distributor){
                product.distributor = distributor;
                product.distributorName = distributor.name;
            });
        }
    };

This works all well, but now i am a bit unsure on if that's the right or 
completely wrong way to do something like that.
What i'm most unsure about is if i'm missing anything on linking objects to 
each other (Product --> Distributor). 
And further, as there is a promised involved for getting the Distributor, 
and i am looping over a list of Products, the scope moves on to the last 
product if i just use a 'product = this', and for that reason i am using 
that closure in getDistributor. 
But i'm not sure if that isn't a bit ugly....

Anyone understood what i was talking about and maybe able to add to my 
understanding?



-- 
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.

Reply via email to