Skip to content

Instantly share code, notes, and snippets.

@rodrimaia
Last active August 29, 2015 13:57
Show Gist options
  • Save rodrimaia/9647775 to your computer and use it in GitHub Desktop.
Save rodrimaia/9647775 to your computer and use it in GitHub Desktop.
CustomBindings for Knockout
/* Bindings */
ko.bindingHandlers.selectPicker = {
init: function(element, valueAccessor, allBindingsAccessor) {
if ($(element).is('select')) {
if (ko.isObservable(valueAccessor())) {
if ($(element).prop('multiple') && $.isArray(ko.utils.unwrapObservable(valueAccessor()))) {
// in the case of a multiple select where the valueAccessor() is an observableArray, call the default Knockout selectedOptions binding
ko.bindingHandlers.selectedOptions.init(element, valueAccessor, allBindingsAccessor);
} else {
// regular select and observable so call the default value binding
ko.bindingHandlers.value.init(element, valueAccessor, allBindingsAccessor);
}
}
$(element).addClass('selectpicker').selectpicker();
}
},
update: function(element, valueAccessor, allBindingsAccessor) {
if ($(element).is('select')) {
var saveValue = null;
var selectPickerOptions = allBindingsAccessor().selectPickerOptions;
if (typeof selectPickerOptions !== 'undefined' && selectPickerOptions !== null) {
var options = selectPickerOptions.optionsArray,
optionsText = selectPickerOptions.optionsText,
optionsValue = selectPickerOptions.optionsValue,
optionsCaption = selectPickerOptions.optionsCaption,
isDisabled = selectPickerOptions.disabledCondition || false,
resetOnDisabled = selectPickerOptions.resetOnDisabled || false;
if (ko.utils.unwrapObservable(options).length > 0) {
// call the default Knockout options binding
saveValue = valueAccessor()();
ko.bindingHandlers.options.update(element, options, allBindingsAccessor);
}
if (isDisabled && resetOnDisabled) {
// the dropdown is disabled and we need to reset it to its first option
$(element).selectpicker('val', $(element).children('option:first').val());
}
$(element).prop('disabled', isDisabled);
}
if (ko.isObservable(valueAccessor())) {
if ($(element).prop('multiple') && $.isArray(ko.utils.unwrapObservable(valueAccessor()))) {
// in the case of a multiple select where the valueAccessor() is an observableArray, call the default Knockout selectedOptions binding
ko.bindingHandlers.selectedOptions.update(element, valueAccessor);
} else {
// call the default Knockout value binding
if (saveValue != null)
valueAccessor()(saveValue);
ko.bindingHandlers.value.update(element, valueAccessor());
}
}
$(element).selectpicker('refresh');
}
}
};
// Here's a custom Knockout binding that makes elements shown/hidden via jQuery's fadeIn()/fadeOut() methods
// Could be stored in a separate utility library
ko.bindingHandlers.visible = {
init: function(element, valueAccessor) {
// Initially set the element to be instantly visible/hidden depending on the value
var value = valueAccessor();
$(element).toggle(ko.unwrap(value)); // Use "unwrapObservable" so we can handle values that may or may not be observable
},
update: function(element, valueAccessor) {
// Whenever the value subsequently changes, slowly fade the element in or out
var value = valueAccessor();
ko.unwrap(value) ? $(element).fadeIn() : $(element).fadeOut();
}
};
ko.bindingHandlers.slider = {
init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
// This will be called when the binding is first applied to an element
// Set up any initial state, event handlers, etc. here
element.addClass('slider').slider();
},
update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
// This will be called once when the binding is first applied to an element,
// and again whenever the associated observable changes value.
// Update the DOM element based on the supplied values here.
element.value = valueAccessor();
}
};
ko.bindingHandlers.date = {
update: function(element, valueAccessor, allBindingsAccessor) {
var value = valueAccessor(), allBindings = allBindingsAccessor();
var valueUnwrapped = ko.utils.unwrapObservable(value);
var d = "";
if (valueUnwrapped) {
var m = /Date\([\d+-]+\)/gi.exec(valueUnwrapped);
if (m) {
d = String.format("{0:dd/MM/yyyy}", eval("new " + m[0]));
}
}
$(element).text(d);
}
};
ko.bindingHandlers.money = {
update: function(element, valueAccessor, allBindingsAccessor) {
var value = valueAccessor(), allBindings = allBindingsAccessor();
var valueUnwrapped = ko.utils.unwrapObservable(value);
var m = "";
if (valueUnwrapped) {
m = parseInt(valueUnwrapped);
if (m) {
m = String.format("{0:n0}", m);
}
}
$(element).text(m);
}
};
/* Fim dos Bindings */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment