Last active
August 29, 2015 13:57
-
-
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 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
// 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