Multiple instances of custom field renderer on page - only first waits for dynamic model.

This is a bit complex, so bear with me.

Ben (awesomely) helped correct some issues with this combination of custom field renderer and dynamically created model.

This field renderer is running five times on the same page, hence the switch/case statement. The problem is that apparently only the first instance is waiting for the Nurse model to be created. The subsequent models seem to think that the Nurse model exists, and run the rendering logic immediately. However, there is nothing in the Nurse model, so their picklists have nothing in them.

What’s the best way to approach a solution? Do I have to separate these into individual field renderers? It seems like they would run into the same problem of the latter ones assuming that the Nurse model exists when it doesn’t. Perhaps my problem is that a new deferred promise is being created for each instance of the field renderer? I’m still fuzzy on the jquery deferred.

Enough speculation. Here’s the code:

custom field renderer:

<br>'renderInitials': function (field, value) { value = skuid.utils.decodeHTML(value);<br> // Helper function with all rendering logic<br> var renderingLogic = function() {<br> if (field.mode === 'edit'){<br> &nbsp; &nbsp;var initials = [],<br> &nbsp; &nbsp; &nbsp; &nbsp;currentRN = value,<br> &nbsp; &nbsp; &nbsp; &nbsp;renderer = false,<br> &nbsp; &nbsp; &nbsp; &nbsp;userId = skuid.utils.userInfo.userId;<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;//get options for initials picklist &nbsp; &nbsp;<br> &nbsp; &nbsp;$.each(nurseModel.getRows(), function(i, row){<br> &nbsp; &nbsp; &nbsp; &nbsp;initials.push({<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;value: row.Initials__c,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;label: row.Initials__c<br> &nbsp; &nbsp; &nbsp; &nbsp;});<br> &nbsp; &nbsp; &nbsp; &nbsp;if (!currentRN &amp;&amp; (row.Id == userId)) {currentRN = row.Initials__c}<br> &nbsp; &nbsp;});<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;//determine if we should set the default initials<br> &nbsp; &nbsp;switch ( {<br> &nbsp; &nbsp; &nbsp; &nbsp;case 'RN_Initials_for_Test__c' :<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;renderer = (field.model.getFieldValue(field.row, 'STD_Test_Method__c')) ? true : false;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp; &nbsp; &nbsp;case 'RN_Initials_for_Results__c' :<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;renderer = (field.model.getFieldValue(field.row, 'Results__c')) ? true : false;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp; &nbsp; &nbsp;case 'RN_Initials_for_Reporting_to_Patient__c' :<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;renderer = (field.model.getFieldValue(field.row, 'Results_Reported_to_Patient__c')) ? true : false;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp; &nbsp; &nbsp;case 'RN_Initials_Reporting_to_Health_Dept__c' :<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;renderer = (field.model.getFieldValue(field.row, 'Results_Reported_to_Health_Department__c')) ? true : false;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp; &nbsp; &nbsp;case 'RN_Initials_for_Treatment__c' :<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;renderer = (field.model.getFieldValue(field.row, 'STD_Treatment__c')) ? true : false;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br> &nbsp; &nbsp;}<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;//render picklist<br> &nbsp; &nbsp;var customSelect = skuid.ui.renderers.PICKLIST.edit({<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;entries : initials,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;required : false,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;defaultValue : (renderer) ? currentRN : ''<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}).change(function() {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// &nbsp;Update the row in the target object<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;field.model.updateRow(field.row,, $(this).val());<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});<br> &nbsp; &nbsp;field.element.append(customSelect);<br> } else {<br> &nbsp; &nbsp;skuid.ui.fieldRenderers.TEXT[field.mode](field,value);<br> }<br> };<br> // Get the Nurses Model<br> var nurseModel = skuid.$M('Nurses');<br> if (!nurseModel) { // If it doesn't exist, create it.<br> var runCreateNurseModel = skuid.snippet.getSnippet('createNurseModel');<br> var waitForModel = runCreateNurseModel();<br> $.when(waitForModel).then(function(resolvedModel){<br> nurseModel = resolvedModel;<br> // Render!<br> renderingLogic();<br> });<br> } else {<br> // Render!<br> renderingLogic(); <br> }<br> }

Dynamic creation of model:

<br>'createNurseModel': function() { // jQuery deferred promise<br>dfd = new $.Deferred();<br>// Create dynamic model to capture nurse<br>nurseModel = new skuid.model.Model();<br>nurseModel.objectName = 'User';<br> = 'Nurses';<br>nurseModel.fields = [<br> &nbsp; &nbsp;{ id: 'Id' },<br> &nbsp; &nbsp;{ id: 'Initials__c' }<br>];<br>nurseModel.orderByClause = 'Initials__c'<br>nurseModel.conditions = [<br> &nbsp; &nbsp;{&nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;type: 'fieldvalue',&nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;field: 'UserRole.Name',&nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;operator: 'contains',&nbsp;<br> &nbsp; &nbsp; &nbsp; &nbsp;value: 'RN',<br> &nbsp; &nbsp; &nbsp; &nbsp;enclosevalueinquotes: true<br> &nbsp; &nbsp;}<br>];<br>$.when(nurseModel.initialize().register().load())<br>.done(function(){<br>dfd.resolve(nurseModel);<br>})<br>.fail(function(){<br>dfd.reject();<br>});<br>return dfd.promise();<br>}

The first picklist renders like this (and obviously takes a second after page load to show up):

The next four render immediately on page load like this:

It seems like the challenge is this:

How to I get all the field renderers to wait for the model to be loaded without asking every one of them to re-create the model?

Do I have to use a settimeout for 200ms or something at the beginning of the rendering logic to keep cycling until the model has rows?

One problem may be that each of these field renderers is independently trying to dynamically create the Model, and they may be interfering with each other in this effort. To correct this you can adjust your createNurseModel function to see if the process of creating a nurse model is already in motion, and if so rather than starting a duplicate iteration of this process, return the same exact promise. Example:

var createNurseModelDfd;

‘createNurseModel’: function() { // jQuery deferred promise
// If we are already in process of creating a nurse model deferred, return the same promise
if (createNurseModelDfd) return createNurseModelDfd.promise();
// Otherwise proceed with creation of the deferred and the dynamic model 
var dfd = createNurseModelDfd = new $.Deferred();

// Create dynamic model to capture nurse
// …


Thanks for the help. Your code, combined with checking for the existence of rows along with the existence of the model, and we’re good to go!

<br>if (nurseModel &amp;&amp; nurseModel.getRows().length) {<br>renderingLogic(); <br>} else { <br>var runCreateNurseModel = skuid.snippet.getSnippet('createNurseModel');<br>var waitForModel = runCreateNurseModel();<br>$.when(waitForModel).then(function(resolvedModel){<br>nurseModel = resolvedModel;<br>// Render!<br>renderingLogic();<br>});<br>}