Mar*_*lon 5 javascript knockout.js
我正在尝试让我的自定义绑定与 observable 和普通对象一起使用。我按照这个问题的答案:
但是,如果我查看执行 allBindingsAccessor 时返回的对象,则属性 '_ko_property_writers' 未定义。
有谁知道这在淘汰赛的第 3 版中是否发生了变化?
编辑
对不起,我应该说,我试图以一种可观察的不可知方式将值“写”回模型
执行此操作的标准方法ko.unwrap如下所述:http ://knockoutjs.com/documentation/custom-bindings.html
例如:
ko.bindingHandlers.slideVisible = {
update: function(element, valueAccessor, allBindings) {
// First get the latest data that we're bound to
var value = valueAccessor();
// Next, whether or not the supplied model property is observable, get its current value
var valueUnwrapped = ko.unwrap(value);
// Grab some more data from another binding property
var duration = allBindings.get('slideDuration') || 400; // 400ms is default duration unless otherwise specified
// Now manipulate the DOM element
if (valueUnwrapped == true)
$(element).slideDown(duration); // Make the element visible
else
$(element).slideUp(duration); // Make the element invisible
}
};
Run Code Online (Sandbox Code Playgroud)
在该示例中,valueUnwrapped无论用户绑定到可观察对象还是普通对象,都是正确的。
| 归档时间: |
|
| 查看次数: |
450 次 |
| 最近记录: |