Skip to content

Instantly share code, notes, and snippets.

@codearachnid
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codearachnid/9418136 to your computer and use it in GitHub Desktop.
Save codearachnid/9418136 to your computer and use it in GitHub Desktop.
This is now managed officially here: https://github.com/codearachnid/knockout.extend
// This is now managed officially here: https://github.com/codearachnid/knockout.extend
// custom binding attrIf which will check the value of a specific observable boolean before add or not the attributes
ko.bindingHandlers.attrIf = {
update: function (element, valueAccessor, allBindingsAccessor) {
var h = ko.utils.unwrapObservable(valueAccessor());
var ifShow = ko.utils.unwrapObservable(h._if);
if (ifShow) {
ko.bindingHandlers.attr.update(element, valueAccessor, allBindingsAccessor);
} else {
for (var k in h) {
if (h.hasOwnProperty(k) && k.indexOf("_") !== 0) {
$(element).removeAttr(k);
}
}
}
}
};
// custom method to bulk shove items into an observableArray
ko.observableArray.fn.pushAll = function (valuesToPush) {
var underlyingArray = this();
this.valueWillMutate();
ko.utils.arrayPushAll(underlyingArray, valuesToPush);
this.valueHasMutated();
return this;
};
//https://github.com/ivaynberg/select2/wiki/Knockout.js-Integration
ko.bindingHandlers.select2 = {
init: function(element, valueAccessor) {
$(element).select2(valueAccessor());
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
$(element).select2('destroy');
});
},
update: function(element) {
$(element).trigger('change');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment