Created
September 12, 2014 02:21
-
-
Save zacwasielewski/5470ec7a6650753f3ba9 to your computer and use it in GitHub Desktop.
Selectize.js plugin to load static options alongside remote options
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Selectize.define('persist_initial_options', function(options) { | |
var self = this, | |
initialOptions = []; | |
this.onInitialize = (function() { | |
var original = self.onInitialize; | |
initialOptions = Object.keys(self.options); | |
})(); | |
this.onSearchChange = (function() { | |
var original = self.onSearchChange; | |
return function(value) { | |
if (!value.length) { | |
Object | |
.keys(self.options) | |
.diff(initialOptions) | |
.forEach(function(id){ | |
self.removeOption(id); | |
}); | |
} | |
return original.apply(self, arguments); | |
}; | |
})(); | |
this.addOption = (function() { | |
var original = self.addOption; | |
return function(data) { | |
return original.apply(self, arguments); | |
}; | |
})(); | |
Array.prototype.diff = function(a) { | |
return this.filter(function(i) {return a.indexOf(i) < 0;}); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment