gpt4 book ai didi

javascript - 链接 Knockout 自定义绑定(bind)

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:16:09 26 4
gpt4 key购买 nike

我有一个 Knockout 自定义绑定(bind)处理程序,我想在其中调用 foreach 绑定(bind)功能,然后调用回调函数。当我尝试执行此操作时,我不断收到“ Uncaught Error :您不能将绑定(bind)多次应用于同一元素。”错误。

我的自定义绑定(bind)非常简单( typescript ):

/// <reference path="knockout.d.ts" />
ko.bindingHandlers["postForeach"] = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
if (!allBindingsAccessor().postForeachCallback)
throw "Callback not defined for postForeach binding!";

//call foreach init functionality
ko.bindingHandlers['foreach'].init(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
//call foreach update functionality
ko.bindingHandlers['foreach'].update(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);
//call callback
allBindingsAccessor().postForeachCallback();
}
};

在构建这个过程中我遗漏了什么吗?

谢谢!

编辑:

回调函数

self.populateMainContentWindow = function () {
var dataTable = $(this.tableId).dataTable();
dataTable.fnDestroy();

// create the datatable
var actualTable = this.jQuery(this.tableId);
if (actualTable.length == 0) {
return false;
}

// create the data table with options
var newDataTable = actualTable.dataTable(this.options);

// always set the width afterwards
actualTable.css("width", "100%");
};

数据绑定(bind)签名(在“with”绑定(bind)中):

postForeach: array, postForeachCallback: $parent.viewModel().populateMainContentWindow

最佳答案

Knockout 使用 init 的返回值来确定它是否应该处理元素的后代。您可以只返回 foreach.init 函数的值,也可以从您的 init 函数中专门返回 { controlsDescendantBindings: true }:

init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
if (!allBindingsAccessor().postForeachCallback)
throw "Callback not defined for postForeach binding!";

//call foreach init functionality
return ko.bindingHandlers['foreach'].init(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext);
},

引用:http://knockoutjs.com/documentation/custom-bindings-controlling-descendant-bindings.html

关于javascript - 链接 Knockout 自定义绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18796261/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com