本文整理汇总了TypeScript中knockout.pureComputed函数的典型用法代码示例。如果您正苦于以下问题:TypeScript pureComputed函数的具体用法?TypeScript pureComputed怎么用?TypeScript pureComputed使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pureComputed函数的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: createToggleButton
export function createToggleButton(title = "Toggle") {
const toggled = ko.observable(Math.random() >= 0.5);
return {
__: "button",
title: ko.pureComputed(() => title + (toggled() ? " (on)" : " (off)")),
icon: getRandomIcon(),
selected: toggled,
click: function() { toggled(!toggled()); }
};
}
示例2: constructor
constructor(public question: QuestionBase) {
var self = this;
question.visibilityChangedCallback = function () { self.onVisibilityChanged(); };
question.renderWidthChangedCallback = function () { self.onRenderWidthChanged(); };
this.koVisible = ko.observable(this.question.visible);
this.koRenderWidth = ko.observable(this.question.renderWidth);
this.koErrors = ko.observableArray();
this.koMarginLeft = ko.pureComputed(function () { self.koRenderWidth(); return self.getIndentSize(self.question.indent); });
this.koPaddingRight = ko.observable(self.getIndentSize(self.question.rightIndent));
this.question["koVisible"] = this.koVisible;
this.question["koRenderWidth"] = this.koRenderWidth;
this.question["koErrors"] = this.koErrors;
this.question["koMarginLeft"] = this.koMarginLeft;
this.question["koPaddingRight"] = this.koPaddingRight;
}
示例3:
export function createComputedArray<T>(value: MaybeSubscribable<any[]>, mapFunction?: (obj: any) => T, context?: any): ko.PureComputed<T[]> {
const cpArray = ko.pureComputed(() => {
if (cpArray[OLD_VALUES_PROPERTY]) {
cpArray[OLD_VALUES_PROPERTY].forEach(d => {
if (typeof d.dispose === "function") {
d.dispose();
}
});
}
const val = ko.unwrap(value);
if (!Array.isArray(val)) {
cpArray[OLD_VALUES_PROPERTY] = null;
return [];
}
return cpArray[OLD_VALUES_PROPERTY] = val.map(mapFunction, context);
});
return cpArray;
}
示例4: purify
export function unpromise<T>(evaluator: () => T | PromiseLike<T>, options?: UnpromiseOptions<T>): ko.PureComputed<T | undefined> {
options = options || {};
const
latestValue = ko.observable(options.initialValue),
errorValue = options.errorValue || options.initialValue,
owner = options.owner,
pureComputed = ko.pureComputed<T | undefined>(latestValue);
let waitingOn = 0;
purify(pureComputed, function () {
var prom = evaluator.call(owner),
current = ++waitingOn;
if (isPromiseLike(prom)) {
prom.then(
res => {
if (current === waitingOn) {
latestValue(res);
}
},
() => {
if (current === waitingOn) {
latestValue(errorValue);
}
});
}
else {
latestValue(prom);
}
});
return pureComputed;
}
示例5: beforeEach
beforeEach(() => {
obs = ko.observable("value");
base = ko.pureComputed<number>(ko.observable(Math.random()));
});
示例6: constructor
constructor(data) {
mapping.fromJS(data, {}, this);
this.nameLength = ko.pureComputed(() => this.name().length, this);
}