i have a route at /campaigns/{show}
which displays a page with an action button 'join campaign'
i.e. /campaigns/{campaignId}
e.g. /campaigns/campaign-1
/apps/templates/campaigns/show.hbs:
...
<button type="button" {{action "joinCampaign"}}>join campaign</button>
...
/apps/controllers/campaigns/show.js:
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
joinCampaign: function() {
// how to do a JSONAPI RESTful POST to /campaigns/{campaignId}/memberships ?
}
}
});
upon clicking the 'join campaign'
button i wish to do a JSONAPI Restful POST to a rest service listening for a POST to /campaigns/{campaignId}/memberships
i have created the corresponding nested route in ember
i.e. /campaigns/{campaignId}/memberships
e.g. /campaigns/campaign-1/memberships
Two possible options:
If you are using ember data, define a model with fields that match your API endpoint, and use this.store.createRecord(...)
to create the model. By default, ember-data will try to "guess" the URL based on some assumptions. Since your assumptions do not match, you will need to customize the url in the adapter.
If you are not using ember-data, you can manually construct the request using Ember.$.ajax
(a convenient built-in alias for jquery).
Note that defining the URL in your ember app is not sufficient: if you want two users on different computers to see each others' saved data, you will need a backend server with a RESTful API, separate from your ember app.