Last active
August 29, 2015 14:26
-
-
Save oliverbarnes/71ced4c6d334680f1c63 to your computer and use it in GitHub Desktop.
Computed property with promise not working
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
var PromiseObject = Ember.ObjectProxy.extend(Ember.PromiseProxyMixin); | |
export default Ember.Component.extend({ | |
proposal: null, | |
participant: null, | |
// previousSupport: [], | |
actions: { | |
toggleSupport: function(proposal, participant){ | |
this.set('proposal', proposal); | |
this.set('participant', participant); | |
var previousSupportQuery = { | |
query: { | |
filter: { | |
proposal_id: this.get('proposal.id'), | |
participant_id: this.get('participant.id') | |
} | |
} | |
}; | |
var self = this | |
this.container.lookup('service:supports').find(previousSupportQuery).then(function(resources) { | |
if( Ember.isEmpty(resources) ) { | |
self.addSupport(); | |
} else { | |
self.removeSupport(resources); | |
} | |
}); | |
} | |
}, | |
addSupport: function() { | |
var support = this.container.lookupFactory('model:supports').create(); | |
support.addRelationship('proposal', this.get('proposal.id')); | |
support.addRelationship('participant', this.get('participant.id')); | |
this.store.createResource('support', support); | |
}, | |
removeSupport: function(resources) { | |
this.store.deleteResource('support', resources.get('firstObject')); | |
} | |
// alreadySupported: Ember.computed('proposal.id', function() { | |
// return !Ember.isEmpty(this.get('previousSupport')); | |
// }), | |
// | |
// previousSupport: Ember.computed('proposal.id', 'participant.id', function() { | |
// var query = { | |
// query: { | |
// filter: { | |
// proposal_id: this.get('proposal.id'), | |
// participant_id: this.get('participant.id') | |
// } | |
// } | |
// }; | |
// | |
// return PromiseObject.create({ | |
// promise: this.container.lookup('service:supports').find(query) | |
// }); | |
// }) | |
}); |
as mentioned in slack return !Ember.isEmpty(this.previousSupport);
needs to use this.get
like so return !Ember.isEmpty(this.get('previousSupport'));
@pixelhandler just went with a callback after the find()
for now, not that bad in the end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@oliver maybe the
alreadySupported
property need be based on another property ofpreviousSupport
maybepreviousSupport.isFulfilled
I'm only guessing http://emberjs.com/api/classes/Ember.PromiseProxyMixin.htmlSee this example, it also uses an ObjectProxy instance - https://github.com/pixelhandler/ember-jsonapi-resources/blob/master/addon/models/resource.js#L391-L405
notice that the
content
still needs to be set after calling.then