Wait for an update to complete and then retrieve the first row?

  • 1
  • Question
  • Updated 5 years ago
  • Answered
We have a wizard and a custom button which does some logic to determine the next step. The logic relies on an "errorModel" returning zero rows. The first step of the wizard allows the user to update the detailModel; the custom action should save the detailModel, update the errorModel, then retrieve the first row. If nothing is returned, the navigation will go to step4; otherwize the navigation will go to step3.

Here's my code:

var $ = skuid.$;
// retrieve the detail model
var IQmodel = skuid.model.getModel('Intake Queues');
// save the detail model
IQmodel.save();
// get the Errors model
var errorModel = skuid.model.getModel('InvalidIQ');
errorModel.updateData({callback: function(){
var row = errorModel.getFirstRow();
// check if any Error records are returned
var wizard = $('.nx-wizard').data('object');
if(row==null) {
// if true, navigate to step3
wizard.skooWizard('navigate','step4');
} else {
// if false, navigate to step2
wizard.skooWizard('navigate','step3');
}
}
});
Photo of Jonathan Adlerstein

Jonathan Adlerstein

  • 528 Points 500 badge 2x thumb

Posted 5 years ago

  • 1
Photo of Jonathan Adlerstein

Jonathan Adlerstein

  • 528 Points 500 badge 2x thumb
The challenge is that the detail model is successfully saved but the errorModel doesn't complete the update in time for the decision on how to navigate. So even though after the update the model will return no rows (and should skip to step4) the wizard has already navigated to step3 by the time the errorModel completes its update.
Photo of Jonathan Adlerstein

Jonathan Adlerstein

  • 528 Points 500 badge 2x thumb
One of my developers solved this:

var $ = skuid.$;
// retrieve the detail model
var IQmodel = skuid.model.getModel('Intake Queues');
// save the detail model
IQmodel.save();
// get the Errors model
var errorModel = skuid.model.getModel('InvalidIQ');
errorModel.updateData();
errorModel.updateData(function() {callbackFunction(errorModel);});

function callbackFunction(errorsModel) {
var row = errorsModel.getFirstRow();
// check if any Error records are returned
var wizard = $('.nx-wizard').data('object');
if(row==null) {
// if true, navigate to step3
wizard.skooWizard('navigate','step4');
} else {
// if false, navigate to step2
wizard.skooWizard('navigate','step3');
}
};
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
Yeah looks like you solved it --- the trick here is syntax. Our The save and updateData methods on Skuid Models have slightly different syntax for specifying a "callback" function to use after the asynchronous operations have completed. The syntax for each is described in our skuid.model API documentation, but basically it's like this:



var contactsModel = skuid.model.getModel('Contacts');
var newRow = myModel.createRow();
contactsModel.updateRow(newRow,{
'FirstName':'George',
'LastName':'Bailey'
});

//
// SAVE
//
contactsModel.save({callback: function(result){
// True if ALL rows saved were inserted, updated, and/or deleted successfully
if (result.totalsuccess) {
// Get the 18-digit Id of our new record
console.log(newRow.Id);
// Get the 15-digit Id
console.log(newRow.Id15);
// Show the entire record
console.log(newRow);
} else {
// Otherwise, you can check out exactly what happened
// with each DML operation, and respond appropriately
console.log(result.insertResults);
console.log(result.updateResults);
console.log(result.deleteResults);
}
}});

//
// UPDATE DATA (reruns the Model's query)
//

// Let's toggle a filterable Condition defined on our Model,
// to just show Contacts in the USA,
// then update our Model so that its query will be rerun

var countryCondition = contactsModel.getConditionByName('MailingCountry');
contactsModel.setCondition(countryCondition,'USA');
contactsModel.updateData(function(){
// Lets see how many rows in our Model now
console.log(contactsModel.data.length);
});