mardi 26 mai 2015

Ember Controller Property, Promise, and Scope

Very new to Javascript and even newer to Ember here, I have an async hasMany association and I'm trying to sum a property on the child model (tournament) in the parent models controller. My understanding of function scope is that the inner functions will have access to everything in the containing functions (except for this), and that's been fine till now. I'm trying to add to amount when the promise resolves, but it must be making a new amount variable because as soon as I make it back to the outter forEach it's back to 0, and I'm not sure where I'm going wrong. I'm aware I'm not setting the dependencies in the .property(), I just wanted to sort this out first.

totalPrice: function(){
    var self = this;
    var allTourn = this.get('model.tournaments');
    var amount = 0;

    allTourn.forEach(function(tournament){
        var tID = Number(tournament.get('id'));

        self.store.find('tournament', tID).then(function(value){
            amount += Number(value.get('buyIn'));
            //amount is set to the buyIn property as expected
        })

        // amount is 0 out here... shouldnt the amount in the promise be using the outter amount variable?
    });

    return amount; // == 0
}.property()




Aucun commentaire:

Enregistrer un commentaire