Snippet to update multiple models at once

  • 1
  • Question
  • Updated 2 years ago
  • Answered
I am using a table filter and model actions to update many models at once. The conditions that are being affected have a blank value and are off by default and all have the same name. I understand the snippet is updating the value and (as far as I can see) not declaring enabling/disabling the condition. 

The curious thing is when you change the option in the filter to a value all the models update correctly but when you set the filter back to "None" only one a set of models have the condition turned off, the other models have the condition on and the value is set to "blank".

Models Update properly:
    skuid.$M('ClassConsult'),
skuid.$M('EDUMarketing'),
skuid.$M('OnlineTraining'),
skuid.$M('FreeTraining'),
skuid.$M('TrainingInstitutionalClients'),
skuid.$M('TrainingRentalHouse'),
skuid.$M('TrainingVendorProvider'),
skuid.$M('TrainingAspiringCreative'),
skuid.$M('TrainingEstablishedProfessional') skuid.$M('AffiliateClass'),
Models Update but condition doesn't turn off:
    skuid.$M('IntegrationNoLabor'),
skuid.$M('RetailSales'),
skuid.$M('OnlineSales'),
skuid.$M('Distribution'),
skuid.$M('SalesShipping'),
skuid.$M('SalesInstitutionalClients'),
skuid.$M('SalesRentalHouse'),
skuid.$M('SalesVendorProvider'),
skuid.$M('SalesEstablishedProfessional'),
skuid.$M('SalesAspiringCreative'),
skuid.$M('StandardRental'),
skuid.$M('ShippingRental'),
skuid.$M('RentalInstitutionalClients'),
skuid.$M('RentalRentalHouse'),
skuid.$M('RentalVendorProvider'),
skuid.$M('RentalEstablishedProfessional'),
skuid.$M('RentalAspiringCreative'), skuid.$M('AccountSales'),

I checked the conditions on each model and they are all set up exactly the same.  I am not sure where else to investigate.


Full Snippet:
var $ = skuid.$;
var officeValue = skuid.$M('OfficeFilters').getConditionByName('OfficeFilter').value;
    
var targetModels = [];
targetModels.push(
    skuid.$M('IntegrationNoLabor'),
    skuid.$M('AccountSales'),
    skuid.$M('RetailSales'),
    skuid.$M('OnlineSales'),
    skuid.$M('Distribution'),
    skuid.$M('SalesShipping'),
    skuid.$M('SalesInstitutionalClients'),
    skuid.$M('SalesRentalHouse'),
    skuid.$M('SalesVendorProvider'),
    skuid.$M('SalesEstablishedProfessional'),
    skuid.$M('SalesAspiringCreative'),
    skuid.$M('StandardRental'),
    skuid.$M('ShippingRental'),
    skuid.$M('RentalInstitutionalClients'),
    skuid.$M('RentalRentalHouse'),
    skuid.$M('RentalVendorProvider'),
    skuid.$M('RentalEstablishedProfessional'),
    skuid.$M('RentalAspiringCreative'),
    skuid.$M('ClassConsult'),
    skuid.$M('AffiliateClass'),
    skuid.$M('EDUMarketing'),
    skuid.$M('OnlineTraining'),
    skuid.$M('FreeTraining'),
    skuid.$M('TrainingInstitutionalClients'),
    skuid.$M('TrainingRentalHouse'),
    skuid.$M('TrainingVendorProvider'),
    skuid.$M('TrainingAspiringCreative'),
    skuid.$M('TrainingEstablishedProfessional'));

$.each(targetModels, function(){
   this.setCondition(this.getConditionByName('OfficeFilter'), officeValue);
});
Photo of Tami Lust

Tami Lust

  • 5,280 Points 5k badge 2x thumb
  • confused

Posted 2 years ago

  • 1
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Tami,

You should probably use a conditional statement to determine if your source condition is active.

Update the beginning with this:

var officeCondition = skuid.$M('OfficeFilters').getConditionByName('OfficeFilter'),
    officeValue = officeCondition.value;

And update the end to this:
if (officeCondition.inactive) { //If the condition is inactive, deactivate all others.
$.each(targetModels, function(){
this.deactivateCondition(this.getConditionByName('OfficeFilter'));
  });
} else { //The condition is active. Activate and Set the values of all the others.
$.each(targetModels, function(){
this.setCondition(this.getConditionByName('OfficeFilter'), officeValue);
  });
}
Photo of Tami Lust

Tami Lust

  • 5,280 Points 5k badge 2x thumb
Matt,

Perfect! Thank you so much.
(Edited)