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 in model after selecting result from Select2 Ajax #285

Open
aruntummala opened this issue May 11, 2015 · 1 comment
Open

Comments

@aruntummala
Copy link

Added ajax config in select2 config , on selecting the result, the added model attribute is undefined .
Here is the fiddle http://jsfiddle.net/atummala/40rwfybf/
On click of save the model attributes are logged.

@zosnap
Copy link

zosnap commented Mar 6, 2020

I don't think anyone is still maintaining this library, but I ran into this issue recently and when I got to the bottom of it turns out it's because stickit sets a jQuery data property called 'stickit-bind-val' in each option tag that it creates or processes and uses that to read back the value when the option is selected. But when select2 creates option tags from your selection it doesn't set that data property (why would it), so stickit has nothing to read back.

In our codebase, I overrode this by modifying the getVal method for 'select' types to check the actual value of the option tag as a fallback if the stickit-bind-val data property isn't set. I can make a pull request if anyone is still around to accept it and agrees that this should be the default behavior.

Here's the snippet:

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

2 participants