本文整理匯總了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);
}