Global Field Renderer Overrides for Individual Fields

This is a continuation of this other thread, but I’m reposting because I’m in a hurry and the other thread was marked ‘Answered’ for some reason.

Simplified use case:

Everywhere the ui finds a picklist field called Primary_Phone_Type__c, run a custom field renderer snippet called “renderPhoneTypes”
Everywhere the ui finds a picklist field called STD__c, run a custom field renderer called “renderSTDs”
Etc.

I have an inline static resource which includes this code:

// Get references to our original picklist field renderers var fieldRenderers = skuid.ui.fieldRenderers;&nbsp;<br>var PICKLIST = fieldRenderers.PICKLIST;<br>// My custom Phone field renderer<br>var checkPicklistRenderer = function(field, value) {<br>//Object as switch statement<br>var globalPickFields = {<br>'Case_Type__c': 'renderCaseTypes',<br>'Primary_Phone_Type__c': 'renderPhoneTypes',<br>'Alternate_Phone_Type__c': 'renderPhoneTypes',<br>'Interaction_Intention__c': 'renderPregnancyIntentions',<br>'Pregnancy_Intention__c': 'renderPregnancyIntentions',<br>'Pregnancy_Intention_on_Intake__c': 'renderPregnancyIntentions',<br>'Father_of_the_Baby_Intention__c': 'renderPregnancyIntentions',<br>'Role__c': 'renderStaffRoles',<br>'Primary_Staff_Role__c': 'renderStaffRoles',<br>'Additional_Staff_Role__c': 'renderStaffRoles',<br>'STD__c': 'renderSTDs',<br>'Sexual_Exposure_to_What_STD__c' : 'renderSTDs',<br>'STD_Picklist': 'renderSTDs',<br>'Referral_Source__c': 'renderReferralSources',<br>'Results__c': 'renderPregnancyTestResults',<br>};<br>// If the field is one of the global picklist fields, run the snippet for it<br>if (globalPickFields[field.id]){<br>skuid.snippet.getSnippet(globalPickFields[field.id])(field,value);<br><b>//*** PROBLEM : when snippets attempt to call standard renderer in edit mode&nbsp;<br></b><b> // they get this checkPicklistRenderer again. Infinite loop.<br></b>} else { //Otherwise run the standard renderer<br>PICKLIST[field.mode](field,value);<br><b>//*** PROBLEM : infinite loop<br></b> &nbsp; }<br>};<br>&nbsp; &nbsp;<b>// Override all of Skuid's standard Picklist field renderers to call our custom renderer<br></b>&nbsp; &nbsp;PICKLIST.edit = function(){<br>&nbsp; &nbsp; &nbsp; &nbsp;checkPicklistRenderer.apply(this,arguments);<br>&nbsp; &nbsp;};

Hey Matt -

Your encountering an infinite loop because in your override of PICKLIST.edit you are calling your custom renderer which then calls PICKLIST so while in edit mode, it will then call your custom renderer again, etc.

What you need to do here is store off the original PICKLIST object in a variable, override PICKLIST and then when you need to call the original PICKLIST, use your variable that contains the original PICKLIST renderer.

Something like:

var origPhoneEdit = fieldRenderers.PICKLIST.edit;

Then in your custom renderer, call the following instead of PICKLIST[field.mode](field, value)

origPhoneEdit.apply(this, arguments);


Makes sense.

I’ve been puzzling over “How can I access the original renderer after I’ve overridden it?”

Store it as a variable, of course!

Thanks, Barry!