Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined object(s) set on FormModel when using select2 with ajax #406

Open
zosnap opened this issue Mar 6, 2020 · 0 comments
Open

Undefined object(s) set on FormModel when using select2 with ajax #406

zosnap opened this issue Mar 6, 2020 · 0 comments

Comments

@zosnap
Copy link

zosnap commented Mar 6, 2020

This is due to an underlying issue with how stickit handles option tags, see: nytimes/backbone.stickit#285

I don't expect that issue to get resolved anytime soon since I don't think that project is maintained, so it would be nice to fix this in Torso. @mandragorn suggested setting it up as a view-level or field-level override, so you can choose whether to allow the fallback to checking the option value directly.

For reference, here is the stickit getVal method for select elements: https://github.com/nytimes/backbone.stickit/blob/master/backbone.stickit.js#L696

And here is how I've overridden and replaced that:

let selectHandler = _.find(Backbone.Stickit._handlers,(h) => {
  return h.selector === 'select';
});
selectHandler.getVal = function($el) {
  var selected = $el.find('option:selected');

  if ($el.prop('multiple')) {
    return _.map(selected, function(el) {
      let stickitVal = Backbone.$(el).data('stickit-bind-val');
      if (stickitVal !== undefined) {
        return stickitVal;
      } else {
        return Backbone.$(el).val();
      }
    });
  } else {
    let stickitVal = selected.data('stickit-bind-val');
    if (stickitVal !== undefined) {
      return stickitVal;
    } else {
      return selected.val();
    }
  }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant