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

Error when preventing 'select2:opening' event #3431

Closed
deje1011 opened this issue Jun 2, 2015 · 3 comments
Closed

Error when preventing 'select2:opening' event #3431

deje1011 opened this issue Jun 2, 2015 · 3 comments

Comments

@deje1011
Copy link

deje1011 commented Jun 2, 2015

First of all, thank you for this plugin! I did not completely cotton up to the new API, yet but all in all it works great and is very flexible.

However I get an error when I try to prevent the dropdown from opening or closing,

uiSelectElement.one('select2:opening', function(e){
    e.preventDefault();
});

Error : Uncaught TypeError: Cannot set property 'prevented' of undefined

  Select2.prototype.trigger = function (name, args) {
    var actualTrigger = Select2.__super__.trigger;
    var preTriggerMap = {
      'open': 'opening',
      'close': 'closing',
      'select': 'selecting',
      'unselect': 'unselecting'
    };

    if (name in preTriggerMap) {
      var preTriggerName = preTriggerMap[name];
      var preTriggerArgs = {
        prevented: false,
        name: name,
        args: args
      };

      actualTrigger.call(this, preTriggerName, preTriggerArgs);

      if (preTriggerArgs.prevented) {
        args.prevented = true; // this is where the error occurs 

        return;
      }
    }

    actualTrigger.call(this, name, args);
  };

Line 5161 (I´m using version 4.0)

It seems like there are no arguments passed when triggering the open / close events.

Thanks in advance,
Jesse

@kevin-brown
Copy link
Member

Part of the issue here is that we do not provide in a blank object ({}) if one wasn't passed in originally.

@althaus
Copy link

althaus commented Oct 20, 2015

@kevin-brown Is it possible to release all the current fixes as 4.0.1 and move the open issues to 4.0.2?

@MusikAnimal
Copy link

For anyone still on Select 4.0.0, you could alternatively use CSS to disable opening/closing Select2:

.select2-selection {
  border-radius: 4px !important;
  pointer-events: none;
}

.select2-selection__choice__remove {
  pointer-events: visible;
}

.select2-dropdown--below {
  display: none;
}

Super hacky but it works, all browsers and IE11+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants