Skip to content

Instantly share code, notes, and snippets.

@oliverbarnes
Created August 7, 2015 16:10
Show Gist options
  • Save oliverbarnes/9f2f875344b4c6443b24 to your computer and use it in GitHub Desktop.
Save oliverbarnes/9f2f875344b4c6443b24 to your computer and use it in GitHub Desktop.
Ember - example of models handling models
// app/templates/components/support-button.hbs
<button {{action 'toggleSupport' proposal session.me}}>
// app/components/support-button.js
export default Ember.Component.extend({
actions: {
toggleSupport: function(proposal, participant){
return proposal.toggleSupportBy(participant);
}
}
});
// app/models/proposals.js
export default Resource.extend({
type: 'proposals',
service: Ember.inject.service('proposals'),
// store: Ember.inject.service('service:store'), //how is this done?
toggleSupportBy: function(participant) {
var query = {
query: {
filter: { proposal_id: this.get('id') },
filter: { participant_id: participant.get('id') }
}
};
var self = this;
this.container.lookup('service:supports').find(query).then(function(response) {
if(Ember.isEmpty(response)) {
self.addSupportBy(participant);
} else {
self.removeSupport(response.get('firstObject'));
}
});
},
addSupportBy: function(participant) {
var support = this.container.lookupFactory('model:supports').create();
support.addRelationship('proposal', this.get('id'));
support.addRelationship('participant', participant);
debugger;
this.store.createResource('support', support);
},
removeSupport: function(support) {
this.store.deleteResource('support', support);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment