Skip to content

Instantly share code, notes, and snippets.

@davelowensohn
Last active October 15, 2016 00:10
Show Gist options
  • Save davelowensohn/4adb71d4e879b7d936433f60d7baf40a to your computer and use it in GitHub Desktop.
Save davelowensohn/4adb71d4e879b7d936433f60d7baf40a to your computer and use it in GitHub Desktop.
AJAX dependent update pattern
// Example 1
updateProviders(next) {
var matrix = this.get('allPatientProviderMatrix');
var curledMatrix = {matrix}; // wrap array in {} to JSONify
var ember = this;
new Promise( function(resolve, reject) {
var localEmber = ember;
Ember.$.ajax({
type: "POST",
url: ENV.fullApiUrl+'/matrix/' + localEmber.get('model.report.id'),
data: JSON.stringify(curledMatrix),
success: function (data){
resolve(data); // on success, goto .then
},
contentType: "application/json",
});
}).then(data => {
this.set('allPatientProviderMatrix', data);
this.send('updateFacilities', next);
});
},
// Example 2
saveAndContinue(routeName) {
var newRegObj = this.get('newRegistrationObject'),
serializedPayload = JSON.stringify(newRegObj),
emberContext = this;
new Promise( function(resolve, reject) {
var localEmber = emberContext;
// console.log('step 1');
$.ajax({
type: "POST",
url: ENV.fullApiUrl+'/auth',
data: serializedPayload,
success: function (data){
// console.log('step 2');
resolve(data); // on success, goto .then
},
contentType: "application/json",
});
}).then(data => {
// console.log('we made it!');
this.transitionToRoute(routeName);
});
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment