KnockoutJS drop unused as

This commit is contained in:
the-djmaze 2024-03-03 16:27:21 +01:00
parent 513fc949d7
commit dde1183174
4 changed files with 11 additions and 29 deletions

View file

@ -13,7 +13,7 @@ ko.bindingHandlers = {};
// The ko.bindingContext constructor is only called directly to create the root context. For child
// contexts, use bindingContext.createChildContext or bindingContext.extend.
ko.bindingContext = class {
constructor(dataItemOrAccessor, parentContext, dataItemAlias, extendCallback, options)
constructor(dataItemOrAccessor, parentContext, extendCallback, options)
{
var self = this,
shouldInheritData = dataItemOrAccessor === inheritParentVm,
@ -60,9 +60,6 @@ ko.bindingContext = class {
self['$data'] = dataItem;
}
if (dataItemAlias)
self[dataItemAlias] = dataItem;
// The extendCallback function is provided when creating a child context or extending a context.
// It handles the specific actions needed to finish setting up the binding context. Actions in this
// function could also add dependencies to this binding context.
@ -107,21 +104,14 @@ ko.bindingContext = class {
// But this does not mean that the $data value of the child context will also get updated. If the child
// view model also depends on the parent view model, you must provide a function that returns the correct
// view model on each update.
'createChildContext'(dataItemOrAccessor, dataItemAlias, extendCallback, options) {
if (!options && dataItemAlias && typeof dataItemAlias == "object") {
options = dataItemAlias;
dataItemAlias = options['as'];
extendCallback = options['extend'];
}
return new ko.bindingContext(dataItemOrAccessor, this, dataItemAlias, (self, parentContext) => {
'createChildContext'(dataItemOrAccessor, options) {
return new ko.bindingContext(dataItemOrAccessor, this, (self, parentContext) => {
// Extend the context hierarchy by setting the appropriate pointers
self['$parentContext'] = parentContext;
self['$parent'] = parentContext['$data'];
self['$parents'] = (parentContext['$parents'] || []).slice(0);
self['$parents'].unshift(self['$parent']);
if (extendCallback)
extendCallback(self);
options['extend']?.(self);
}, options);
}
@ -129,7 +119,7 @@ ko.bindingContext = class {
// Similarly to "child" contexts, provide a function here to make sure that the correct values are set
// when an observable view model is updated.
'extend'(properties, options) {
return new ko.bindingContext(inheritParentVm, this, null, self =>
return new ko.bindingContext(inheritParentVm, this, self =>
ko.utils.extend(self, typeof(properties) == "function" ? properties(self) : properties)
, options);
}
@ -432,7 +422,7 @@ ko.storedBindingContextForNode = node => {
function getBindingContext(viewModelOrBindingContext, extendContextCallback) {
return viewModelOrBindingContext && (viewModelOrBindingContext instanceof ko.bindingContext)
? viewModelOrBindingContext
: new ko.bindingContext(viewModelOrBindingContext, undefined, undefined, extendContextCallback);
: new ko.bindingContext(viewModelOrBindingContext, null, extendContextCallback);
}
ko['applyBindingAccessorsToNode'] = (node, bindings, viewModelOrBindingContext) =>

View file

@ -16,8 +16,7 @@ ko.bindingHandlers['foreach'] = {
// 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'],
'as': unwrappedValue['as']
'foreach': unwrappedValue['data']
};
},
'init': (element, valueAccessor) =>

View file

@ -7,7 +7,7 @@ function makeWithIfBinding(bindingKey, isWith, isNot) {
var savedNodes, contextOptions = {}, needAsyncContext;
if (isWith) {
contextOptions = { 'as': allBindings.get('as'), 'exportDependencies': true };
contextOptions = { 'exportDependencies': true };
}
needAsyncContext = allBindings['has'](ko.bindingEvent.descendantsComplete);

View file

@ -115,7 +115,7 @@
// Ensure we've got a proper binding context to work with
var bindingContext = (dataOrBindingContext instanceof ko.bindingContext)
? dataOrBindingContext
: new ko.bindingContext(dataOrBindingContext, null, null, null, { "exportDependencies": true });
: new ko.bindingContext(dataOrBindingContext, null, null, { "exportDependencies": true });
var templateName = resolveTemplateName(template, bindingContext['$data'], bindingContext);
executeTemplate(targetNodeOrNodeArray, renderMode, templateName, bindingContext, options);
@ -130,19 +130,13 @@
ko.renderTemplateForEach = (template, arrayOrObservableArray, options, targetNode, parentBindingContext) => {
// Since setDomNodeChildrenFromArrayMapping always calls executeTemplateForArrayItem and then
// activateBindingsCallback for added items, we can store the binding context in the former to use in the latter.
var arrayItemContext, asName = options['as'];
var arrayItemContext;
// This will be called by setDomNodeChildrenFromArrayMapping to get the nodes to add to targetNode
var executeTemplateForArrayItem = (arrayValue, index) => {
// Support selecting template as a function of the data being rendered
arrayItemContext = parentBindingContext['createChildContext'](arrayValue, {
'as': asName,
'extend': context => {
context['$index'] = index;
if (asName) {
context[asName + "Index"] = index;
}
}
'extend': context => context['$index'] = index
});
var templateName = resolveTemplateName(template, arrayValue, arrayItemContext);
@ -267,7 +261,6 @@
var innerBindingContext = bindingContext;
if ('data' in options) {
innerBindingContext = bindingContext['createChildContext'](options['data'], {
'as': options['as'],
'exportDependencies': true
});
}