0

我有以下问题。以下代码适用于 JS:

Application1.Trackers = function (params) {

var viewModel = {
    dsTrackers: new DevExpress.data.DataSource({
        store: Application1.db,
        searchExpr: "Bezeichnung"
    }),

    searchString: ko.observable(''),
    find: function () {
        viewModel.showSearch(!viewModel.showSearch());
        viewModel.searchString('');
    },
    showSearch: ko.observable(false),
};

ko.computed(function () {
    return viewModel.searchString();
}).extend({
    throttle: 500
}).subscribe(function (value) {
    viewModel.dsTrackers.filter("Bezeichnung", "contains", value);
    viewModel.dsTrackers.pageIndex(0);
    viewModel.dsTrackers.load();
});

return viewModel;

};

在打字稿中,我尝试过这种方式,但这不起作用:

module MyExtremeApp {
export function Trackers(params: { id: any }) {
    return {
        dsTrackers: new DevExpress.data.DataSource({
            store: MyGlobals.oTrackerManager.getTrackerCustomStore(),
            searchExpr: "Bezeichnung"
        }),

        searchString: ko.observable(''),
        find: function () {
            this.showSearch(!this.showSearch());
            this.searchString('');
        },
        showSearch: ko.observable(false),
    };

    ko.computed(() => {
        return this.searchString();
    }).extend({
        throttle: 500
    }).subscribe(function (value) {
        this.dsTrackers.filter("Bezeichnung", "contains", value);
        this.dsTrackers.pageIndex(0);
        this.dsTrackers.load();
    });
}

}

它永远不会跳转到 ko.computed()。有谁知道为什么不? 我是打字稿的新手

非常感谢。此致

4

1 回答 1

0

由于您正在转向 TypesSript,因此您应该使用 opertunity 来思考面向对象并使用类和模块。这样您就可以生成更具可读性的代码。您的课程可能如下所示:

import mm_data = require('pathToMyDataClass');
import mm_globals = require('pathToMyGlobals');

export interface ITrackerParms {
    id: any;
}
export class Trackers {
    constructor(parms: ITrackerParms) {
        this.parms = parms;
        this.dsTrackers = new DevExpress.data.DataSource({
            store: mm_globals.MyGlobals.oTrackerManager.getTrackerCustomStore(),
            searchExpr: "Bezeichnung"
        });
        this.searchString.extend({ throttle: 500 });
        this.searchString.subscribe((val: string) => {
            this.dsTrackers.filter("Bezeichnung", "contains", val);
            this.dsTrackers.pageIndex(0);
            this.dsTrackers.load();
        }, this);
    }
    public parms: ITrackerParms;
    public dsTrackers: mm_data.ITrackerSource; //interface from your datasource module
    public showSearch = ko.observable(false);
    public searchString = ko.observable('');
    public find() : void {
        this.showSearch(!this.showSearch());
        this.searchString('');
    }
}

但是您的问题是基于确保在类的构造函数中设置订阅和计算值(或者在您的示例中等于构造函数)。事实上,您不需要在示例中使用 compuded,因为您可以扩展 Knockout observable 以进行节流。

于 2015-04-29T17:20:56.137 回答