Skip to content

Commit

Permalink
set bindingcontext in constructor function
Browse files Browse the repository at this point in the history
  • Loading branch information
Auge19 committed Jan 22, 2025
1 parent 1695010 commit 3dfc00a
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 7 deletions.
3 changes: 1 addition & 2 deletions global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,7 @@ declare global {
export interface BindingContext<T = any> {
ko: any; // typeof ko;

[name: string]: any;

[symbol: symbol]: any
$parent?: any;
$parents: any[];
$root: any;
Expand Down
6 changes: 3 additions & 3 deletions packages/bind/src/bindingContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface BindingContextSetting {

// The bindingContext constructor is only called directly to create the root context. For child
// contexts, use bindingContext.createChildContext or bindingContext.extend.
export function bindingContext (dataItemOrAccessor: any, parentContext?: any, dataItemAlias?: any, extendCallback?: Function, settings?: BindingContextSetting) {
export function bindingContext (dataItemOrAccessor: any, parentContext?: BindingContext, dataItemAlias?: string, extendCallback?: Function, settings?: BindingContextSetting) {
const self = this
const shouldInheritData = dataItemOrAccessor === inheritParentIndicator
const realDataItemOrAccessor = shouldInheritData ? undefined : dataItemOrAccessor
Expand Down Expand Up @@ -139,7 +139,7 @@ Object.assign(bindingContext.prototype, {
// 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: any, dataItemAlias: any, extendCallback?: Function, settings?: BindingContextSetting) {
createChildContext (dataItemOrAccessor: any, dataItemAlias?: string, extendCallback?: Function, settings?: BindingContextSetting) {
return new bindingContext(dataItemOrAccessor, this, dataItemAlias, function (self, parentContext) {
// Extend the context hierarchy by setting the appropriate pointers
self.$parentContext = parentContext
Expand All @@ -158,7 +158,7 @@ Object.assign(bindingContext.prototype, {
extend (properties) {
// If the parent context references an observable view model, "_subscribable" will always be the
// latest view model object. If not, "_subscribable" isn't set, and we can use the static "$data" value.
return new bindingContext(inheritParentIndicator, this, null, function (self, parentContext) {
return new bindingContext(inheritParentIndicator, this, undefined, function (self, parentContext) {
extend(self, typeof properties === 'function' ? properties.call(self) : properties)
})
},
Expand Down
2 changes: 1 addition & 1 deletion packages/binding.template/src/templating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export function renderTemplate (template, dataOrBindingContext, options, targetN
// Ensure we've got a proper binding context to work with
var bindingContext = (dataOrBindingContext && (dataOrBindingContext instanceof BindingContextConstructor))
? dataOrBindingContext
: new BindingContextConstructor(dataOrBindingContext, null, null, undefined, { 'exportDependencies': true })
: new BindingContextConstructor(dataOrBindingContext, undefined, undefined, undefined, { 'exportDependencies': true })

var templateName = resolveTemplateName(template, bindingContext.$data, bindingContext)
const renderedNodesArray = executeTemplate(targetNodeOrNodeArray, renderMode, templateName, bindingContext, options, afterBindingCallback)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ describe('all bindings', function () {

describe('The lookup of variables (get_lookup_root)', function () {
function makeBindings (binding, context, globals?, node?) {
const ctx = new bindingContext(context, null, null)
const ctx = new bindingContext(context)
return new Parser().parse(binding, ctx, globals, node)
}

Expand Down

0 comments on commit 3dfc00a

Please sign in to comment.