Created
June 12, 2012 20:58
-
-
Save WinstonFassett/2920096 to your computer and use it in GitHub Desktop.
Common Knockout Bindings
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
// from https://github.com/SteveSanderson/knockout/wiki/Bindings---class | |
// addresses https://github.com/SteveSanderson/knockout/issues/233 | |
// be careful of using the attr binding -- it has issues with iOS and quotes | |
// instead, use this to bind to the class attribute | |
ko.bindingHandlers['class'] = { | |
'update': function (element, valueAccessor) { | |
if (element['__ko__previousClassValue__']) { | |
$(element).removeClass(element['__ko__previousClassValue__']); | |
} | |
var value = ko.utils.unwrapObservable(valueAccessor()); | |
$(element).addClass(value); | |
element['__ko__previousClassValue__'] = value; | |
} | |
}; | |
// 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.fadeVisible = { | |
init: function (element, valueAccessor) { | |
// Initially set the element to be instantly visible/hidden depending on the value | |
var value = valueAccessor(); | |
$(element).toggle(ko.utils.unwrapObservable(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.utils.unwrapObservable(value) ? $(element).fadeIn() : $(element).fadeOut('fast'); | |
} | |
}; | |
ko.bindingHandlers.slideVisible = { | |
init: function (element, valueAccessor) { | |
// Initially set the element to be instantly visible/hidden depending on the value | |
var value = valueAccessor(); | |
$(element).toggle(ko.utils.unwrapObservable(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.utils.unwrapObservable(value) ? $(element).slideDown('fast') : $(element).slideUp('fast'); | |
} | |
}; | |
ko.bindingHandlers.log = { | |
init: function (element, valueAccessor, allBindingsAccessor) { | |
}, | |
update: function (element, valueAccessor) { | |
var value = ko.utils.unwrapObservable(valueAccessor()); | |
console.log("observable changed", value); | |
} | |
} | |
// from http://www.knockmeout.net/2011/06/lazy-loading-observable-in-knockoutjs.html | |
//an observable that retrieves its value when first bound | |
ko.onDemandObservable = function (callback, target) { | |
var _value = ko.observable(); //private observable | |
var requesting = false; | |
var result = ko.dependentObservable({ | |
read: function () { | |
//if it has not been loaded, execute the supplied function | |
if (!result.loaded() && !requesting) { | |
// console.log('loading observable'); | |
requesting = true; | |
callback.call(target); | |
} | |
//always return the current value | |
return _value(); | |
}, | |
write: function (newValue) { | |
//indicate that the value is now loaded and set it | |
// console.log('observable loaded!'); | |
requesting = false; | |
result.loaded(true); | |
_value(newValue); | |
}, | |
deferEvaluation: true //do not evaluate immediately when created | |
}); | |
//expose the current state, which can be bound against | |
result.loaded = ko.observable(); | |
//load it again | |
result.refresh = function () { | |
result.loaded(false); | |
}; | |
return result; | |
}; | |
ko.bindingHandlers.hidden = { | |
update: function (element, valueAccessor) { | |
var value = ko.utils.unwrapObservable(valueAccessor()); | |
ko.bindingHandlers.visible.update(element, function () { return !value; }); | |
} | |
}; | |
ko.bindingHandlers.allowBindings = { | |
init: function(elem, valueAccessor) { | |
// Let bindings proceed as normal *only if* my value is false | |
var shouldAllowBindings = ko.utils.unwrapObservable(valueAccessor()); | |
return { controlsDescendantBindings: !shouldAllowBindings }; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment