【发布时间】:2016-02-09 02:52:19
【问题描述】:
我想问是否可以重用远程绑定kendo.data.Datasource,而不是使用新参数创建新绑定。
我的场景是我有一个搜索框和一个列表视图。当用户进入搜索框时,会向服务器发送一个带有搜索框值参数的请求。我能够通过创建新的kendo.data.Datasource 每个keyup 事件使其正常工作,但内存成为我的一个问题。
var viewModel = kendo.observable({
searchId: "", // searchbox value
searchResult: null, // search result listview datasource
searchFnc: function() { // search function
// QUESTION: is there any way to update the current datasource object
// to refresh the list view instead of create new object?
// something like:
// this.set("searchResult.options.transport.read.data.postId", this.get("searchId"));
this.set("searchResult", new kendo.data.DataSource({
transport: {
read: {
url: "http://jsonplaceholder.typicode.com/comments",
dataType: "jsonp",
data: {
postId: this.get("searchId")
}
}
}
}));
}
});
kendo.bind($("#myView"), viewModel);
.item {
list-style: none;
}
.item span {
display: inline-block;
min-width: 40px;
}
#myListView {
min-height: 50px;
}
<link href="https://kendo.cdn.telerik.com/2016.1.112/styles/kendo.common.min.css" rel="stylesheet" />
<link href="https://kendo.cdn.telerik.com/2016.1.112/styles/kendo.default.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2016.1.112/js/kendo.all.min.js"></script>
<div id="myView">
<label>Enter Post ID (1,2,3,4...)
<label>
<br/>
<span class="k-textbox k-space-right">
<input type="text" data-value-update="keyup" data-bind="value: searchId, events: {keyup: searchFnc}"/>
<a href="javascript:;" class="k-icon k-i-search" data-bind="click: searchFnc"> </a>
</span>
<ul id="myListView" data-role="listview" data-bind="source: searchResult" data-template="template-search-result">
</ul>
</div>
<script type="text/x-kendo-template" id="template-search-result">
<li class="item">
<span>#: postId #</span>
<span>#: id #</span>
<span>#: name #</span>
</li>
</script>
【问题讨论】:
标签: javascript jquery kendo-ui kendo-datasource