如果模型属性是ko.observable(),则可以在自定义绑定中按如下方式访问它们。
var observable = valueAccessor();
使用Knockout-ES5插件时如何获取自定义绑定中的observable?检查下面的代码并查找评论“如何获取propertyName在这里?”
JS Fiddle when not using Knockout-ES plugin由Another Look at Custom Bindings for KnockoutJS
提供Updated fiddle with model changed to use Knockout-ES plugin
ko.bindingHandlers.datepicker = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
//initialize datepicker with some optional options
var options = allBindingsAccessor().datepickerOptions || {};
$(element).datepicker(options);
//handle the field changing
ko.utils.registerEventHandler(element, "change", function () {
var observable = valueAccessor();
if (!ko.isObservable(observable)) {
console.log("Not Observable");
//How to get propertyName here?
//ko.getObservable(viewModel, 'propertyName');
return;
}
observable($(element).datepicker("getDate"));
});
//handle disposal (if KO removes by the template binding)
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
$(element).datepicker("destroy");
});
},
//update the control when the view model changes
update: function(element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor()),
current = $(element).datepicker("getDate");
if (value - current !== 0) {
$(element).datepicker("setDate", value);
}
}
};
var viewModel = {
myDate: new Date("11/01/2011"),
setToCurrentDate: function() {
this.myDate = new Date();
}
};
ko.track(viewModel);
ko.applyBindings(viewModel);
答案 0 :(得分:2)
您可以将实际的observable传递给绑定:
data-bind="datepicker: ko.getObservable($data, 'myDate') ..."
但那很难看。幸运的是,Knockout确实提供了一种方法(未记录)来从绑定中写入属性值:
//handle the field changing
ko.utils.registerEventHandler(element, "change", function () {
var writable = valueAccessor();
if (!ko.isObservable(writable)) {
var propWriters = allBindingsAccessor()._ko_property_writers;
if (propWriters && propWriters.datepicker) {
writable = propWriters.datepicker;
} else {
return;
}
}
writable($(element).datepicker("getDate"));
});
答案 1 :(得分:1)
我最终使用传递属性名称并回退到_ko_property_writers的组合。 JS Fiddle
<强> HTML 强>
<input data-bind="datepicker: myDate, datePropertyName: 'myDate',
datepickerOptions: { minDate: new Date() }" />
<强>的Javascript 强>
ko.bindingHandlers.datepicker = {
init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
var _viewModel = viewModel;
//initialize datepicker with some optional options
var options = allBindingsAccessor().datepickerOptions || {};
$(element).datepicker(options);
//handle the field changing
ko.utils.registerEventHandler(element, "change", function () {
var observable = valueAccessor();
if (!ko.isObservable(observable)) {
console.log("Not Observable");
var datePropertyName = allBindingsAccessor().datePropertyName;
if (datePropertyName) {
console.log("Using datePropertyName");
observable = ko.getObservable(_viewModel, datePropertyName);
}
else {
console.log("No datePropertyName, trying _ko_property_writers");
var propWriters = allBindingsAccessor()._ko_property_writers;
if (propWriters && propWriters.datepicker) {
observable = propWriters.datepicker;
} else {
console.log("No way to get observable");
return;
}
}
}
observable($(element).datepicker("getDate"));
});
//handle disposal (if KO removes by the template binding)
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
$(element).datepicker("destroy");
});
},
//update the control when the view model changes
update: function(element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor()),
current = $(element).datepicker("getDate");
if (value - current !== 0) {
$(element).datepicker("setDate", value);
}
}
};
var viewModel = {
myDate: new Date("11/01/2011"),
setToCurrentDate: function() {
this.myDate = new Date();
}
};
ko.track(viewModel);
ko.applyBindings(viewModel);
答案 2 :(得分:1)
作为Michael Best的答案的补充,如果您将knockoutjs 3.0与es5插件一起使用,则无法使用allBindingsAccessor()._ko_property_writers;
访问observable
但是我发现可以使用ko.getObservable
函数访问它。所以看起来应该是这样的:
ko.bindingHandlers.myCustomBinder = {
init: function (element, valueAccessor, allBindingsAccessor, context) {
var observable = ko.getObservable(context, 'observableName');
}
}
答案 3 :(得分:0)
好吧,您可以使用预处理来放置getObservable并在绑定处理程序中正常使用它。
preprocess: function (markup: string) {
console.log("markup", markup);
var lastDotIndex = markup.lastIndexOf('.');
if (lastDotIndex > 0) {
var obj = markup.substring(0, lastDotIndex);
var property = markup.substring(lastDotIndex + 1);
return 'ko.getObservable(' + obj + ', \'' + property + '\');';
} else {
return 'ko.getObservable($data, \'' + markup + '\')';
}
},
答案 4 :(得分:0)
function es5Preprocess(val){
var alt = val.split('.');
var tail = alt.pop();
if(alt.length == 0) alt.push('$data');
tail = 'ko.getObservable( ' + alt.join('.') + ', ' + '\'' + tail + '\'' + ' )';
return tail + ' || ' + val;
};
window.ko.es5.customBinding = function(name, binding){
binding.preprocess = es5Preprocess;
window.ko.bindingHandlers[name] = binding;
};
然后
window.ko.es5.customBinding('contentEditable', {
init: function(element, valueAccessor) {
var val = valueAccessor();
element.onblur = function() {
val(element.textContent);
};
},
update: function(element, valueAccessor) {
element.textContent = valueAccessor()();
}
});