Kendo MVVM 自定义小部件:接受对象作为参数

Kendo MVVM custom widget: accept object as parameter

在开发自定义 Kendo MVVM 小部件时,传递一个简单的数据绑定参数似乎没问题,无论是在自定义绑定中。即:

<div data-bind="value: simpleParameter">This works fine</div>

<div data-bind="mybinding: simpleParameter">This also works fine</div>

我注意到 cssevents 绑定可以接受对象作为参数。我也很想接受对象作为参数,但是当我尝试时,它会抛出一个错误:

<div data-role="mycomponent" data-bind="value: { prop: value }">This throws</div>

<div data-role="mycomponent" data-bind="mybinding: { prop: value }">This throws too</div>

在自定义绑定的情况下,在我尝试访问该值之前它不会抛出。我已经试过了...

var arg = this.bindings["mybinding"].get();

...和其他变体,但似乎没有任何效果。是否可以在其 MVVM 框架中为自定义 Kendo UI 小部件接受像 { prop: value, prop2: value2 } 这样的对象?

你的答案在这篇文章中,Making Kendo UI Binders for Complex Types

If you have tried to make a binder like this yourself, you have probably run into issues. Kendo actually does not provide support for these complex bind paths for custom binders.

基本上,Kendo UI 希望您的表达式是变量或函数名称的字符串表示形式,而不是复杂对象。您需要做的是多次调用 this.bindings.class.get() 来检索您需要的值。

要让 get() 函数读取这些值,您将在绑定的 init() 构造函数方法中从复杂对象创建一个 key/value 对列表。然后,在绑定的 refresh() 方法中,您只需遍历此列表并调用 get().