mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-07-13 03:27:39 +03:00
KnockoutJS cleanup code a bit
This commit is contained in:
parent
b07bf3ad12
commit
0e1a34f067
2 changed files with 23 additions and 22 deletions
|
|
@ -12,7 +12,8 @@ ko.bindingHandlers['checked'] = {
|
||||||
// Treat "value" like "checkedValue" when it is included with "checked" binding
|
// Treat "value" like "checkedValue" when it is included with "checked" binding
|
||||||
if (allBindings['has']('checkedValue')) {
|
if (allBindings['has']('checkedValue')) {
|
||||||
return ko.utils.unwrapObservable(allBindings.get('checkedValue'));
|
return ko.utils.unwrapObservable(allBindings.get('checkedValue'));
|
||||||
} else if (useElementValue) {
|
}
|
||||||
|
if (useElementValue) {
|
||||||
return allBindings['has']('value')
|
return allBindings['has']('value')
|
||||||
? ko.utils.unwrapObservable(allBindings.get('value'))
|
? ko.utils.unwrapObservable(allBindings.get('value'))
|
||||||
: element.value;
|
: element.value;
|
||||||
|
|
|
||||||
|
|
@ -1,28 +1,28 @@
|
||||||
// "foreach: someExpression" is equivalent to "template: { foreach: someExpression }"
|
// "foreach: someExpression" is equivalent to "template: { foreach: someExpression }"
|
||||||
// "foreach: { data: someExpression, afterAdd: myfn }" is equivalent to "template: { foreach: someExpression, afterAdd: myfn }"
|
// "foreach: { data: someExpression }" is equivalent to "template: { foreach: someExpression }"
|
||||||
|
const makeTemplateValueAccessor = valueAccessor =>
|
||||||
|
() => {
|
||||||
|
var modelValue = valueAccessor(),
|
||||||
|
// Unwrap without setting a dependency here
|
||||||
|
unwrappedValue = ko.isObservable(modelValue) ? modelValue.peek() : modelValue;
|
||||||
|
|
||||||
|
// If unwrappedValue is the array, pass in the wrapped value on its own
|
||||||
|
// The value will be unwrapped and tracked within the template binding
|
||||||
|
// (See https://github.com/SteveSanderson/knockout/issues/523)
|
||||||
|
if ((!unwrappedValue) || Array.isArray(unwrappedValue))
|
||||||
|
return { 'foreach': modelValue };
|
||||||
|
|
||||||
|
// If unwrappedValue.data is the array, preserve all relevant options and unwrap again value so we get updates
|
||||||
|
ko.utils.unwrapObservable(modelValue);
|
||||||
|
return {
|
||||||
|
'foreach': unwrappedValue['data']
|
||||||
|
};
|
||||||
|
};
|
||||||
ko.bindingHandlers['foreach'] = {
|
ko.bindingHandlers['foreach'] = {
|
||||||
makeTemplateValueAccessor: valueAccessor =>
|
|
||||||
() => {
|
|
||||||
var modelValue = valueAccessor(),
|
|
||||||
// Unwrap without setting a dependency here
|
|
||||||
unwrappedValue = ko.isObservable(modelValue) ? modelValue.peek() : modelValue;
|
|
||||||
|
|
||||||
// If unwrappedValue is the array, pass in the wrapped value on its own
|
|
||||||
// The value will be unwrapped and tracked within the template binding
|
|
||||||
// (See https://github.com/SteveSanderson/knockout/issues/523)
|
|
||||||
if ((!unwrappedValue) || typeof unwrappedValue.length == "number")
|
|
||||||
return { 'foreach': modelValue };
|
|
||||||
|
|
||||||
// If unwrappedValue.data is the array, preserve all relevant options and unwrap again value so we get updates
|
|
||||||
ko.utils.unwrapObservable(modelValue);
|
|
||||||
return {
|
|
||||||
'foreach': unwrappedValue['data']
|
|
||||||
};
|
|
||||||
},
|
|
||||||
'init': (element, valueAccessor) =>
|
'init': (element, valueAccessor) =>
|
||||||
ko.bindingHandlers['template']['init'](element, ko.bindingHandlers['foreach'].makeTemplateValueAccessor(valueAccessor))
|
ko.bindingHandlers['template']['init'](element, makeTemplateValueAccessor(valueAccessor))
|
||||||
,
|
,
|
||||||
'update': (element, valueAccessor, allBindings, viewModel, bindingContext) =>
|
'update': (element, valueAccessor, allBindings, viewModel, bindingContext) =>
|
||||||
ko.bindingHandlers['template']['update'](element, ko.bindingHandlers['foreach'].makeTemplateValueAccessor(valueAccessor), allBindings, viewModel, bindingContext)
|
ko.bindingHandlers['template']['update'](element, makeTemplateValueAccessor(valueAccessor), allBindings, viewModel, bindingContext)
|
||||||
};
|
};
|
||||||
ko.virtualElements.allowedBindings['foreach'] = true;
|
ko.virtualElements.allowedBindings['foreach'] = true;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue