Select2 field plugin making unnecessary ajax calls

Select2 field plugin making unnecessary ajax calls

mguinnessmguinness Posts: 34Questions: 8Answers: 0

I'm using the Select2 plugin for Editor, but I've noticed that when configured to use ajax it makes unneccesary calls like http://localhost/api/Lookup?initialValue=true&value="" to the endpoint during create.

I looked at the plugin code and I think there's a simple solution within the set function. Changing if ( needAjax ) { … } to if ( needAjax && val ) { … } stops the endpoint being called when the requested value is empty.

Hoping that this can be included in the next release.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 49,501Questions: 1Answers: 7,286 Site admin
    Answer ✓

    Agreed - thanks for the suggestion. Its committed in for the next release.

    Allan

  • mguinnessmguinness Posts: 34Questions: 8Answers: 0

    Great, thanks! Would you be able to send me the latest version via email so I can test changes before release?

  • allanallan Posts: 49,501Questions: 1Answers: 7,286 Site admin

    Its just exactly as you suggest at the moment. I think there are one or two other outstanding changes that I need to make. On the plus side, the plug-ins aren't tied to the Editor release, so they can be updated at any time if required.

    Allan

  • mguinnessmguinness Posts: 34Questions: 8Answers: 0

    FYI, when the multiple option is used with Select2 an empty selection comes through as [""] instead of [] so I had to tweak the test to accommodate that.

    if ( needAjax && val && $(val).not([""]).length ) { … }

  • mguinnessmguinness Posts: 34Questions: 8Answers: 0

    This is fixed for a single values in the latest release, but It's still an issue for multiple values (as outlined in my Feb 27th post).

  • allanallan Posts: 49,501Questions: 1Answers: 7,286 Site admin

    when the multiple option is used with Select2 an empty selection comes through as [""]

    That's odd. To my mind that means something is selected and it as an empty string value, which is perfectly valid. I would have expected Select2 to just return [] if there were nothing selected.

    Allan

  • mguinnessmguinness Posts: 34Questions: 8Answers: 0

    Thanks for your reply Allan. It occurs with Standalone model. Say you have the following element:

    <span id="state" data-editor-field="stateCode"></span>

    And you have the following editor field defined:

    label: "State:",
    name: "stateCode",
    type: "select2",
    opts: {
        multiple: true,
        ajax: {
            url: '/api/StateNames',
            dataType: 'json'
        }
    },
    separator: ","
    

    When you go into edit mode it will call /api/States?initialValue=true&value=[""] since JSON.stringify() is used to create the value parameter. I had suggested using $.param() to avoid double quotes but you had concerns for backwards compatibility.

  • allanallan Posts: 49,501Questions: 1Answers: 7,286 Site admin

    It could be that I just need to go with it and call it a v2 of the plug-in or something. I'll investigate some more. Thanks for the insights!

    Allan

  • mguinnessmguinness Posts: 34Questions: 8Answers: 0

    To maintain backwards compatibility an option like
    jsonParam: true|false (with default as true) could work.

  • mguinnessmguinness Posts: 34Questions: 8Answers: 0

    Any chance that $(val).not([""]).length can be included in the select2 plugin code as an interim measure? It would avoid a lot of unnecessary ajax calls on complex forms.

  • allanallan Posts: 49,501Questions: 1Answers: 7,286 Site admin

    That would be an option yes. I do like your jsonParam option, but you could stick that in as an interim solution in your locally hosted code.

    Allan

  • mguinnessmguinness Posts: 34Questions: 8Answers: 0

    @allan Have you given any more thought to adding a jsonParam to the Select2 plugin? I think other people would find this helpful when using the multiple option. Do you see any downside to this change other than the work involved at your end?

  • mguinnessmguinness Posts: 34Questions: 8Answers: 0
    edited May 14

    Suggested code change below to avoid any misunderstanding. For backwards compatibility the original JSON.stringify would be the default if option not specified.

    // Add an initial data request to the server, but don't
    // override `data` since the dev might be using that
    if (conf.jsonParam || true) {
      var initData = 'initialValue=true&value='+JSON.stringify(val);
    } else {
      var initData = $.param({ initialValue: true, value: val }, true);
    }
    
Sign In or Register to comment.