在Durandal.js中激活Ajax调用

时间:2014-08-13 06:29:39

标签: javascript ajax knockout.js durandal

我想根据输入标记中输入的数据刷新页面上的数据。我正在使用Durandal.js。

除了使用composition.addBinding和绑定到jQuery之外,还有其他方法(更优雅)吗?也许使用Knockout?

我想在编辑search_for时触发ajax调用

我的JS是这样的:

define(['plugins/http', 'durandal/app', 'knockout', 'moment'], function (http, app, ko, moment) {
//Note: This module exports an object.
//That means that every module that "requires" it will get the same object instance.
//If you wish to be able to create multiple instances, instead export a function.
//See the "welcome" module for an example of function export.

return {
    start_date: ko.observable(""),
    end_date: ko.observable(""),
    search_for: ko.observable(""),
    records_per_page: ko.observable(""),
    records: ko.observableArray([]),
    getShippersData: function() {
         ....
    }

全部谢谢

1 个答案:

答案 0 :(得分:0)

您可以在此处使用淘汰赛订阅。

define(['plugins/http', 'durandal/app', 'knockout', 'moment'], function (http, app, ko, moment) {
//Note: This module exports an object.
//That means that every module that "requires" it will get the same object instance.
//If you wish to be able to create multiple instances, instead export a function.
//See the "welcome" module for an example of function export.

var self = this;
self.search_for = ko.observable(""); // can make use of rateLimit extender here to avoid frequent ajax calls.

search_for.subscribe(function(searchTerm){
    //make use of searchTerm i.e. search_for variable to make ajax call.
   //Whenever you change value of search_for, this method will invoke
})

return {
    start_date: ko.observable(""),
    end_date: ko.observable(""),
    search_for: search_for,

    records_per_page: ko.observable(""),
    records: ko.observableArray([]),
    getShippersData: function() {
         ....
    }

只要您在search_for变量中更改内容,就会调用订阅。您可以通过使用"速率限制器"设置更改跟踪的限制来限制发生。 (http://knockoutjs.com/documentation/rateLimit-observable.html

self.search_for = ko.observable("").extend({ rateLimit : 500 })

rateLimit 会导致observable在指定的时间段内抑制和延迟更改通知,而不是立即通知更改