在TypeScript中计算(ko.computed in TypeScript)

编程入门 行业动态 更新时间:2024-10-26 14:33:34
TypeScript中计算(ko.computed in TypeScript)

我有以下问题。 以下代码适用于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;

};

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

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()。 有没有人知道为什么不呢? 我是打字稿的新手

非常感谢你。 最好的祝福

I've the following problem. The following code works in 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;

};

In Typescript I tried it this way, but this doesn't work:

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(); }); }

}

It never jumps into ko.computed(). Does anyone has an idea why not? I'm new to typescript

Thank you very much. Best Regards

最满意答案

由于您要转移到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(''); } }

但是你的问题是基于确保在类的构造函数中设置subscrptions和computed(或者在你的例子中等于构造函数)。 事实上,你不需要在你的例子中使用compuded,因为你可以扩展Knockout observable以进行throtteling。

Since you are moving to TypesSript you should use the opertunity to think object oriented and use classes and modules. That way you produce much more readable code. Your class could look like this:

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(''); } }

But your problem is based on making sure that the subscrptions and computeds are set in the constructor of the class (or what in your example equals constructor). In fact you do not need to use compuded in your example since you can extend the Knockout observable for throtteling.

更多推荐

本文发布于:2023-07-15 05:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110614.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:TypeScript   ko   computed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!