我正在尝试以编程方式将事件绑定到字段。“data-bind > events > change” 属性被添加到 DOM 元素中就好了。例如,这是我的脚本生成的 HTML:
<select id="MainContent_DetailsContent_TypeOfLoss" class="valid" name="TypeOfLoss" data-role="dropdownlist" data-text-field="text" data-value-field="value" data-bind="source: TypeOfLoss, events: { change: TypeOfLoss_updateSourceUsingValue }" style="display: none;">
但是,当我更改的值DropDownList
(例如)时,我收到以下错误:
TypeError: handler is not a function
path/to/app/Assets/Lib/kendo/src/js/kendo.web.js
Line 6923
我不知道发生了什么 -TypeOfLoss_updateSourceUsingValue
方法/属性确实存在于相应的视图模型(KendoDOMBinder._viewModel)中。
有任何想法吗?我正在使用 kendoui.web.2013.1.319。
KendoDOMBinder: function (viewModel) {
var binder = Object.create({
_viewModel: {},
_widgets: {},
_root: {},
init: function (viewModel) {
this._viewModel = viewModel || Object.create(kendo.observable());
this._widgets = App.Config.Widgets.defaults();
return this;
},
bindField: function (node, bindings) {
var that = this,
val = [],
ds,
events = [],
event,
target,
fnCallback,
callback;
// Empty data attributes will crash Kendo
if (App.Utilities.isEmpty(bindings) === false) {
// TODO: Should be using strategy pattern to process data attributes
$.each(bindings, function (type, binding) {
switch (type) {
case 'source':
ds = App.Data.DataSource.Factory(binding)
val.push(type + ': ' + node.name);
that._viewModel.set(node.name, ds);
break;
case 'value':
// Make sure the property doesn't exist before adding it
if (that._viewModel.hasOwnProperty(binding) === false) {
// Add the property
that._viewModel.set(binding, '');
// TODO: accept function
}
break;
case 'events':
break;
case 'field':
event = binding.event || 'change';
type = binding.type;
target = binding.target;
if (binding.type == 'triggerUpdate') {
fnCallback = [node.name, binding.callback].join('_');
//console.log(fnCallback);
// Make sure the property doesn't exist before adding it
if (that._viewModel.hasOwnProperty(fnCallback) === false) {
// Define the callback
switch (binding.callback) {
case 'updateSourceUsingValue':
// Add the property
fnCallback = that._viewModel.set(fnCallback, function () {
alert("Doing something");
});
//console.log(that._viewModel);
events.push(event + ': ' + fnCallback);
break;
case 'updateUsingValue':
break;
}
}
}
break;
default:
val.push(type + ': ' + binding);
break;
}
});
if (events.length > 0) {
val.push('events: { ' + events.join(', ') + ' }');
}
}
console.log(val.join(', '));
node.setAttribute('data-bind', val.join(', '));
return this;
},
unbindField: function (key) {
//this._viewModel.unbind;
return this;
},
bind: function (selector) {
selector = selector || 'body';
kendo.bind($(selector), this._viewModel);
},
createFunction: function (ns, fn) {
var nsArray = ns.split(/\./),
currentNode = this._root,
newNS;
while (nsArray.length > 1) {
newNS = nsArray.shift();
if (typeof currentNode[newNS] === "undefined") {
currentNode[newNS] = {};
}
currentNode = currentNode[newNS];
}
if (fn) {
currentNode[nsArray.shift()] = fn;
} else {
currentNode[nsArray.shift()] = {};
}
}
});
return binder.init(viewModel);
}