Created
May 27, 2015 22:49
-
-
Save mryellow/6b2bf67e91d91f54d250 to your computer and use it in GitHub Desktop.
KnockoutJS binding handlers
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
/** | |
* KnockoutJS Binding handler to select from array correct index based on stored key. | |
* This allows a model with `model_id` set to properly select the correct option after binding, when they become available. | |
*/ | |
ko.bindingHandlers.optionsSelect = { | |
init: function(element, valueAccessor, allBindingsAccessor, deprecated, bindingContext) { | |
//console.log('optionsSelect: ', ko.unwrap(valueAccessor()), ko.unwrap(allBindingsAccessor()), bindingContext); | |
var input = ko.unwrap(valueAccessor().input); | |
var output = ko.unwrap(valueAccessor().output); | |
var condition = ko.unwrap(valueAccessor().condition); | |
var options = ko.unwrap(allBindingsAccessor().options); | |
if (input && options && output) { | |
bindingContext.$data[output] = ko.utils.arrayFirst(options, function(item) { | |
if (condition) { | |
var match = null; | |
ko.utils.objectForEach(condition, function(key, val) { | |
if (match !== false) match = (input[key] === item[val]); | |
}); | |
return match; | |
} else { | |
return (item._id === input); | |
} | |
}); | |
// We're doing initial setup, reset dirty. | |
if (bindingContext.$data._isDirty !== undefined) bindingContext.$data._isDirty = false; | |
if (bindingContext.$parent._isDirty !== undefined) bindingContext.$parent._isDirty = false; | |
} | |
} | |
}; | |
/** | |
* KnockoutJS Binding Handler for jQuery-UI date-picker initalisation. | |
*/ | |
ko.bindingHandlers.datePicker = { | |
init: function(element, valueAccessor, allBindingsAccessor, deprecated, bindingContext) { | |
$(element).datepicker({ | |
dateFormat: '@' // Unix Epoch in miliseconds. | |
}); | |
} | |
}; | |
Author
mryellow
commented
Jul 1, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment