Find model based on arguments[0] in custom field renderer

  • 1
  • Question
  • Updated 4 years ago
I have a custom field renderer I'm making for phone fields. In the snippet, I need to get the Id of the record. This wouldn't be a problem if I was only using this on one model, but I'll be using it on all phone fields. That said, I have to dynamically pull in the record Id from the model in which the custom field renderer is used.
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb

Posted 4 years ago

  • 1
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
This it?

var field = arguments[0]
, value = arguments[1]
, field.Model;
Photo of mB Pat Vachon

mB Pat Vachon, Champion

  • 42,714 Points 20k badge 2x thumb
WOOOT! WOOOOOT!!!! Some of this code is original from me!!!!
           
, fmodel = field.model            //get row of the field that was clicked
            , frow = field.row
            , waid = fmodel.getFieldValue(frow,'Id')
            ;



var field = arguments[0]

                , value = arguments[1]

                , decodedValue = skuid.utils.decodeHTML(value)

                , renderer = 
skuid.ui.fieldRenderers[field.metadata.displaytype]

                , mode = field.mode

                , $ = skuid.$; // get a shorthand reference to jquery

// we want to use the default skuid rendering since we are not doing
// anything special to the information displayed in this field
// need to call decode because the 'value' from arguments[1] is escaped
// but the renderer needs the unescaped value as it will escape it internally

renderer[mode](field, decodedValue);

// find the first anchor - there should only be one but just being safe

var anchor = $(field.element).find('a:first');

// if we found an anchor (we won't find one when in edit mode)

if (anchor && anchor.length) {

    // set the href attribute value

    $(anchor).attr('href', 'skype:' + decodedValue);

    // add event listener for click
    // NOTE - This doesn't handle cases like right click or keyboard
    // events so if those are needed, this needs to be adjusted.  This will
    // only handle left-mouse click

    $(anchor).on('click', function () {

       var csModel = skuid.model.getModel('SkuidPageSettings')

            //get first row from SkuidPageSettings
            , csRow = csModel && csModel.getFirstRow()
            //get model from the field that was clicked
            , fmodel = field.model
            //get row of the field that was clicked
            , frow = field.row
            , waid = fmodel.getFieldValue(frow,'Id')
            ;

        // make sure we found the row
        if (csRow) {

            // for information on initiatorid see - https://community.skuidify.com/skuid/topics/upgraded-to-summer-ga-and-this-line-now-prevents-a-skuid-page-from-being-shown
            csModel.updateRow(csRow, 'waid__c', waid, { initiatorId: field._GUID });

            //for boolean data types, you can use the following
            csModel.updateRow(csRow, 'Checked__c', false, { initiatorId: field._GUID } );
            
            /*
            if you want to update more than one field at a time, use the following construct
            var rowUpdates = { // create a javascript object and set object key/value pairs
            Checked__c : false
            , waid__c : waid
            };
            csModel.updateRows(row, rowUpdates, { initiatorId: field._GUID });
            */
        }

    });

}