Skip to content

Instantly share code, notes, and snippets.

@davelowensohn
Last active December 8, 2016 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davelowensohn/edde4d4c222ccf466e22b60c7e9335a1 to your computer and use it in GitHub Desktop.
Save davelowensohn/edde4d4c222ccf466e22b60c7e9335a1 to your computer and use it in GitHub Desktop.
Hard-fought Ember promise CP that actually resolves in template
{{model.allVariantsQty.value}}
allVariantsQty: Ember.computed('productVariants.@each.inventoryQuantity', function (){
var result = this.get('productVariants').then(resolved => {
var reducedQty = resolved.map( aVar => aVar.get('inventoryQuantity')).reduce((a,b) => a + b, 0);
return Ember.RSVP.resolve({value:reducedQty});
});
return DS.PromiseObject.create({
promise:result
});
}),
@davelowensohn
Copy link
Author

This is the Holy Grail, for getting deep references on the model (or wherever) that play nice with templates. In this case, product.productVariant.aggregatedQuantity. Seems simple, but it's a real hassle, because templates aren't promise-aware & you're "supposed" to do this stuff on the route's model hook.

Further reading:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment