何时在Kendo UI中使用数据源和Observable来显示模板中的数据

When to use in Kendo UI Data-Source and Observable to show Data in a Template

本文关键字:显示 数据 Observable 数据源 Kendo UI 何时      更新时间:2023-09-26

这里有示例代码,它以两种方式显示自动完成。

<div data-role="view" data-model="foo">
    <div>
    An autocomplete widget
    <input data-role="autocomplete" data-source="['foo', 'bar', 'baz']" />
    </div>
    <div>
    A widget bound to the mobile view ViewModel dataSource field
    <input data-role="autocomplete" data-bind="source: dataSource" />
    </div>
</div>
<script>
    var foo = kendo.observable({
        dataSource: ['foo', 'bar', 'baz']
    });
    new kendo.mobile.Application();
</script>

我什么时候必须使用observable,什么时候必须用DataSource?我不明白其中的区别。

您感到困惑的原因是,在您的示例中,您根本没有使用Kendo UI DataSource对象。你的两个例子都是简单的ObservableObjects。要创建DataSource,实际上必须使用kendo.data.DataSource()函数实例化一个变量。

ObservableObjects支持更改跟踪,并在发生更改时通知任何订阅者。

DataSource的项是ObservableObject实例。

Kendo API参考:ObservableObject

Kendo API参考:DataSource