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> var initials = t],<br> currentRN = value,<br> renderer = false,<br> userId = skuid.utils.userInfo.userId;<br> <br> //get options for initials picklist <br> $.each(nurseModel.getRows(), function(i, row){<br> initials.push({<br> value: row.Initials__c,<br> label: row.Initials__c<br> });<br> if (!currentRN && (row.Id == userId)) {currentRN = row.Initials__c}<br> });<br> <br> //determine if we should set the default initials<br> switch (field.id) {<br> case 'RN_Initials_for_Test__c' :<br> renderer = (field.model.getFieldValue(field.row, 'STD_Test_Method__c')) ? true : false;<br> break;<br> case 'RN_Initials_for_Results__c' :<br> renderer = (field.model.getFieldValue(field.row, 'Results__c')) ? true : false;<br> break;<br> case 'RN_Initials_for_Reporting_to_Patient__c' :<br> renderer = (field.model.getFieldValue(field.row, 'Results_Reported_to_Patient__c')) ? true : false;<br> break;<br> case 'RN_Initials_Reporting_to_Health_Dept__c' :<br> renderer = (field.model.getFieldValue(field.row, 'Results_Reported_to_Health_Department__c')) ? true : false;<br> break;<br> case 'RN_Initials_for_Treatment__c' :<br> renderer = (field.model.getFieldValue(field.row, 'STD_Treatment__c')) ? true : false;<br> break;<br> }<br> <br> //render picklist<br> var customSelect = skuid.ui.renderers.PICKLIST.edit({<br> entries : initials,<br> required : false,<br> defaultValue : (renderer) ? currentRN : ''<br> }).change(function() {<br> // Update the row in the target object<br> field.model.updateRow(field.row, field.id, $(this).val());<br> });<br> field.element.append(customSelect);<br> } else {<br> skuid.ui.fieldRenderers.TEXTefield.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>nurseModel.id = 'Nurses';<br>nurseModel.fields = i<br> { id: 'Id' },<br> { id: 'Initials__c' }<br>];<br>nurseModel.orderByClause = 'Initials__c'<br>nurseModel.conditions = t<br> { <br> type: 'fieldvalue', <br> field: 'UserRole.Name', <br> operator: 'contains', <br> value: 'RN',<br> enclosevalueinquotes: true<br> }<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: