UNPKG

43 kBTypeScriptView Raw
1interface KnockoutSubscribableFunctions<T> {
2 /**
3 * Notify subscribers of knockout "change" event. This doesn't actually change the observable value.
4 * @param eventValue A value to be sent with the event.
5 * @param event The knockout event.
6 */
7 notifySubscribers(eventValue?: T, event?: "change"): void;
8 /**
9 * Notify subscribers of a knockout or user defined event.
10 * @param eventValue A value to be sent with the event.
11 * @param event The knockout or user defined event name.
12 */
13 notifySubscribers<U>(eventValue: U, event: string): void;
14}
15
16interface KnockoutComputedFunctions<T> {
17}
18
19interface KnockoutObservableFunctions<T> {
20 /**
21 * Used by knockout to decide if value of observable has changed and should notify subscribers. Returns true if instances are primitives, and false if are objects.
22 * If your observable holds an object, this can be overwritten to return equality based on your needs.
23 * @param a previous value.
24 * @param b next value.
25 */
26 equalityComparer(a: T, b: T): boolean;
27}
28
29// The functions of observable arrays that don't mutate the array
30interface KnockoutReadonlyObservableArrayFunctions<T> {
31 /**
32 * Returns the index of the first occurrence of a value in an array.
33 * @param searchElement The value to locate in the array.
34 * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
35 */
36 indexOf(searchElement: T, fromIndex?: number): number;
37 /**
38 * Returns a section of an array.
39 * @param start The beginning of the specified portion of the array.
40 * @param end The end of the specified portion of the array.
41 */
42 slice(start: number, end?: number): T[];
43}
44// The functions of observable arrays that mutate the array
45interface KnockoutObservableArrayFunctions<T> extends KnockoutReadonlyObservableArrayFunctions<T> {
46 /**
47 * Removes and returns all the remaining elements starting from a given index.
48 * @param start The zero-based location in the array from which to start removing elements.
49 */
50 splice(start: number): T[];
51 /**
52 * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
53 * @param start The zero-based location in the array from which to start removing elements.
54 * @param deleteCount The number of elements to remove.
55 * @param items Elements to insert into the array in place of the deleted elements.
56 */
57 splice(start: number, deleteCount: number, ...items: T[]): T[];
58 /**
59 * Removes the last value from the array and returns it.
60 */
61 pop(): T;
62 /**
63 * Adds new item or items to the end of array.
64 * @param items Items to be added.
65 */
66 push(...items: T[]): void;
67 /**
68 * Removes the first value from the array and returns it.
69 */
70 shift(): T;
71 /**
72 * Inserts new item or items at the beginning of the array.
73 * @param items Items to be added.
74 */
75 unshift(...items: T[]): number;
76 /**
77 * Reverses the order of the array and returns the observableArray (not the underlying array).
78 */
79 reverse(): KnockoutObservableArray<T>;
80 /**
81 * Sorts the array contents and returns the observableArray.
82 */
83 sort(): KnockoutObservableArray<T>;
84 /**
85 * Sorts the array contents and returns the observableArray.
86 * @param compareFunction A function that returns negative value if first argument is smaller, positive value if second is smaller, or zero to treat them as equal.
87 */
88 sort(compareFunction: (left: T, right: T) => number): KnockoutObservableArray<T>;
89
90 // Ko specific
91 /**
92 * Replaces the first value that equals oldItem with newItem.
93 * @param oldItem Item to be replaced.
94 * @param newItem Replacing item.
95 */
96 replace(oldItem: T, newItem: T): void;
97 /**
98 * Removes all values that equal item and returns them as an array.
99 * @param item The item to be removed.
100 */
101 remove(item: T): T[];
102 /**
103 * Removes all values and returns them as an array.
104 * @param removeFunction A function used to determine true if item should be removed and fasle otherwise.
105 */
106 remove(removeFunction: (item: T) => boolean): T[];
107 /**
108 * Removes all values that equal any of the supplied items.
109 * @param items Items to be removed.
110 */
111 removeAll(items: T[]): T[];
112 /**
113 * Removes all values and returns them as an array.
114 */
115 removeAll(): T[];
116
117 // Ko specific Usually relevant to Ruby on Rails developers only
118 /**
119 * Finds any objects in the array that equal someItem and gives them a special property called _destroy with value true.
120 * @param item Items to be marked with the property.
121 */
122 destroy(item: T): void;
123 /**
124 * Finds any objects in the array filtered by a function and gives them a special property called _destroy with value true.
125 * @param destroyFunction A function used to determine which items should be marked with the property.
126 */
127 destroy(destroyFunction: (item: T) => boolean): void;
128 /**
129 * Finds any objects in the array that equal suplied items and gives them a special property called _destroy with value true.
130 * @param items
131 */
132 destroyAll(items: T[]): void;
133 /**
134 * Gives a special property called _destroy with value true to all objects in the array.
135 */
136 destroyAll(): void;
137}
138
139interface KnockoutSubscribableStatic {
140 fn: KnockoutSubscribableFunctions<any>;
141
142 new <T>(): KnockoutSubscribable<T>;
143}
144
145interface KnockoutSubscription {
146 /**
147 * Terminates a subscription.
148 */
149 dispose(): void;
150}
151
152interface KnockoutSubscribable<T> extends KnockoutSubscribableFunctions<T> {
153 /**
154 * Registers to be notified after the observable's value changes.
155 * @param callback Function that is called whenever the notification happens.
156 * @param target Defines the value of 'this' in the callback function.
157 * @param event The knockout event name.
158 */
159 subscribe(callback: (newValue: T) => void, target?: any, event?: "change"): KnockoutSubscription;
160 /**
161 * Registers to be notified before the observable's value changes.
162 * @param callback Function that is called whenever the notification happens.
163 * @param target Defines the value of 'this' in the callback function.
164 * @param event The knockout event name.
165 */
166 subscribe(callback: (newValue: T) => void, target: any, event: "beforeChange"): KnockoutSubscription;
167 /**
168 * Registers to be notified when a knockout or user defined event happens.
169 * @param callback Function that is called whenever the notification happens. eventValue can be anything. No relation to underlying observable.
170 * @param target Defines the value of 'this' in the callback function.
171 * @param event The knockout or user defined event name.
172 */
173 subscribe<U>(callback: (eventValue: U) => void, target: any, event: string): KnockoutSubscription;
174 /**
175 * Customizes observables basic functionality.
176 * @param requestedExtenders Name of the extender feature and its value, e.g. { notify: 'always' }, { rateLimit: 50 }
177 */
178 extend(requestedExtenders: { [key: string]: any; }): KnockoutSubscribable<T>;
179 /**
180 * Gets total number of subscribers.
181 */
182 getSubscriptionsCount(): number;
183 /**
184 * Gets number of subscribers of a particular event.
185 * @param event Event name.
186 */
187 getSubscriptionsCount(event: string): number;
188}
189
190interface KnockoutComputedStatic {
191 fn: KnockoutComputedFunctions<any>;
192
193 /**
194 * Creates computed observable.
195 */
196 <T>(): KnockoutComputed<T>;
197 /**
198 * Creates computed observable.
199 * @param evaluatorFunction Function that computes the observable value.
200 * @param context Defines the value of 'this' when evaluating the computed observable.
201 * @param options An object with further properties for the computed observable.
202 */
203 <T>(evaluatorFunction: () => T, context?: any, options?: KnockoutComputedOptions<T>): KnockoutComputed<T>;
204 /**
205 * Creates computed observable.
206 * @param options An object that defines the computed observable options and behavior.
207 * @param context Defines the value of 'this' when evaluating the computed observable.
208 */
209 <T>(options: KnockoutComputedDefine<T>, context?: any): KnockoutComputed<T>;
210}
211
212interface KnockoutReadonlyComputed<T> extends KnockoutReadonlyObservable<T> {
213 /**
214 * Returns whether the computed observable may be updated in the future. A computed observable is inactive if it has no dependencies.
215 */
216 isActive(): boolean;
217 /**
218 * Returns the current number of dependencies of the computed observable.
219 */
220 getDependenciesCount(): number;
221}
222
223interface KnockoutComputed<T> extends KnockoutReadonlyComputed<T>, KnockoutObservable<T>, KnockoutComputedFunctions<T> {
224 fn: KnockoutComputedFunctions<any>;
225
226 /**
227 * Manually disposes the computed observable, clearing all subscriptions to dependencies.
228 * This function is useful if you want to stop a computed observable from being updated or want to clean up memory for a
229 * computed observable that has dependencies on observables that wont be cleaned.
230 */
231 dispose(): void;
232 /**
233 * Customizes observables basic functionality.
234 * @param requestedExtenders Name of the extender feature and it's value, e.g. { notify: 'always' }, { rateLimit: 50 }
235 */
236 extend(requestedExtenders: { [key: string]: any; }): KnockoutComputed<T>;
237}
238
239interface KnockoutObservableArrayStatic {
240 fn: KnockoutObservableArrayFunctions<any>;
241
242 <T>(value?: T[] | null): KnockoutObservableArray<T>;
243}
244
245/**
246 * While all observable arrays are writable at runtime, this type is analogous to the native ReadonlyArray type:
247 * casting an observable array to this type expresses the intention that it shouldn't be mutated.
248 */
249interface KnockoutReadonlyObservableArray<T> extends KnockoutReadonlyObservable<ReadonlyArray<T>>, KnockoutReadonlyObservableArrayFunctions<T> {
250 // NOTE: Keep in sync with KnockoutObservableArray<T>, see note on KnockoutObservableArray<T>
251 subscribe(callback: (newValue: KnockoutArrayChange<T>[]) => void, target: any, event: "arrayChange"): KnockoutSubscription;
252 subscribe(callback: (newValue: T[]) => void, target: any, event: "beforeChange"): KnockoutSubscription;
253 subscribe(callback: (newValue: T[]) => void, target?: any, event?: "change"): KnockoutSubscription;
254 subscribe<U>(callback: (newValue: U) => void, target: any, event: string): KnockoutSubscription;
255}
256
257/*
258 NOTE: In theory this should extend both KnockoutObservable<T[]> and KnockoutReadonlyObservableArray<T>,
259 but can't since they both provide conflicting typings of .subscribe.
260 So it extends KnockoutObservable<T[]> and duplicates the subscribe definitions, which should be kept in sync
261*/
262interface KnockoutObservableArray<T> extends KnockoutObservable<T[]>, KnockoutObservableArrayFunctions<T> {
263 subscribe(callback: (newValue: KnockoutArrayChange<T>[]) => void, target: any, event: "arrayChange"): KnockoutSubscription;
264 subscribe(callback: (newValue: T[]) => void, target: any, event: "beforeChange"): KnockoutSubscription;
265 subscribe(callback: (newValue: T[]) => void, target?: any, event?: "change"): KnockoutSubscription;
266 subscribe<U>(callback: (newValue: U) => void, target: any, event: string): KnockoutSubscription;
267
268 extend(requestedExtenders: { [key: string]: any; }): KnockoutObservableArray<T>;
269}
270
271interface KnockoutObservableStatic {
272 fn: KnockoutObservableFunctions<any>;
273
274 <T>(value: T): KnockoutObservable<T>;
275 <T = any>(value: null): KnockoutObservable<T | null>
276 <T = any>(): KnockoutObservable<T | undefined>
277}
278
279/**
280 * While all observable are writable at runtime, this type is analogous to the native ReadonlyArray type:
281 * casting an observable to this type expresses the intention that this observable shouldn't be mutated.
282 */
283interface KnockoutReadonlyObservable<T> extends KnockoutSubscribable<T>, KnockoutObservableFunctions<T> {
284 (): T;
285
286 /**
287 * Returns the current value of the computed observable without creating a dependency.
288 */
289 peek(): T;
290 valueHasMutated?: { (): void; };
291 valueWillMutate?: { (): void; };
292}
293
294interface KnockoutObservable<T> extends KnockoutReadonlyObservable<T> {
295 (value: T): void;
296
297 // Since .extend does arbitrary thing to an observable, it's not safe to do on a readonly observable
298 /**
299 * Customizes observables basic functionality.
300 * @param requestedExtenders Name of the extender feature and it's value, e.g. { notify: 'always' }, { rateLimit: 50 }
301 */
302 extend(requestedExtenders: { [key: string]: any; }): KnockoutObservable<T>;
303}
304
305interface KnockoutComputedOptions<T> {
306 /**
307 * Makes the computed observable writable. This is a function that receives values that other code is trying to write to your computed observable.
308 * Its up to you to supply custom logic to handle the incoming values, typically by writing the values to some underlying observable(s).
309 * @param value Value being written to the computer observable.
310 */
311 write?(value: T): void;
312 /**
313 * Disposal of the computed observable will be triggered when the specified DOM node is removed by KO.
314 * This feature is used to dispose computed observables used in bindings when nodes are removed by the template and control-flow bindings.
315 */
316 disposeWhenNodeIsRemoved?: Node;
317 /**
318 * This function is executed before each re-evaluation to determine if the computed observable should be disposed.
319 * A true-ish result will trigger disposal of the computed observable.
320 */
321 disposeWhen?(): boolean;
322 /**
323 * Defines the value of 'this' whenever KO invokes your 'read' or 'write' callbacks.
324 */
325 owner?: any;
326 /**
327 * If true, then the value of the computed observable will not be evaluated until something actually attempts to access its value or manually subscribes to it.
328 * By default, a computed observable has its value determined immediately during creation.
329 */
330 deferEvaluation?: boolean;
331 /**
332 * If true, the computed observable will be set up as a purecomputed observable. This option is an alternative to the ko.pureComputed constructor.
333 */
334 pure?: boolean;
335}
336
337interface KnockoutComputedDefine<T> extends KnockoutComputedOptions<T> {
338 /**
339 * A function that is used to evaluate the computed observables current value.
340 */
341 read(): T;
342}
343
344interface KnockoutBindingContext {
345 $parent: any;
346 $parents: any[];
347 $root: any;
348 $data: any;
349 $rawData: any | KnockoutObservable<any>;
350 $index?: KnockoutObservable<number>;
351 $parentContext?: KnockoutBindingContext;
352 $component: any;
353 $componentTemplateNodes: Node[];
354
355 /**
356 * Clones the current Binding Context, adding extra properties to it.
357 * @param properties object with properties to be added in the binding context.
358 */
359 extend(properties: { [key: string]: any; } | (() => { [key: string]: any; })): KnockoutBindingContext;
360 /**
361 * This returns a new binding context whose viewmodel is the first parameter and whose $parentContext is the current bindingContext.
362 * @param dataItemOrAccessor The binding context of the children.
363 * @param dataItemAlias An alias for the data item in descendant contexts.
364 * @param extendCallback Function to be called.
365 * @param options Further options.
366 */
367 createChildContext(dataItemOrAccessor: any, dataItemAlias?: string, extendCallback?: Function, options?: { "exportDependencies": boolean }): any;
368}
369
370interface KnockoutAllBindingsAccessor {
371 (): any;
372 get(name: string): any;
373 has(name: string): boolean;
374}
375
376interface KnockoutBindingHandler<E extends Node = any, V = any, VM = any> {
377 after?: Array<string>;
378 init?: (element: E, valueAccessor: () => V, allBindingsAccessor: KnockoutAllBindingsAccessor, viewModel: VM, bindingContext: KnockoutBindingContext) => void | { controlsDescendantBindings: boolean; };
379 update?: (element: E, valueAccessor: () => V, allBindingsAccessor: KnockoutAllBindingsAccessor, viewModel: VM, bindingContext: KnockoutBindingContext) => void;
380 options?: any;
381 preprocess?: (value: string, name: string, addBindingCallback?: (name: string, value: string) => void) => string;
382 [s: string]: any;
383}
384
385interface KnockoutBindingHandlers {
386 [bindingHandler: string]: KnockoutBindingHandler;
387
388 // Controlling text and appearance
389 visible: KnockoutBindingHandler;
390 text: KnockoutBindingHandler;
391 html: KnockoutBindingHandler;
392 css: KnockoutBindingHandler;
393 style: KnockoutBindingHandler;
394 attr: KnockoutBindingHandler;
395
396 // Control Flow
397 foreach: KnockoutBindingHandler;
398 if: KnockoutBindingHandler;
399 ifnot: KnockoutBindingHandler;
400 with: KnockoutBindingHandler;
401
402 // Working with form fields
403 click: KnockoutBindingHandler;
404 event: KnockoutBindingHandler;
405 submit: KnockoutBindingHandler;
406 enable: KnockoutBindingHandler;
407 disable: KnockoutBindingHandler;
408 value: KnockoutBindingHandler;
409 textInput: KnockoutBindingHandler;
410 hasfocus: KnockoutBindingHandler;
411 checked: KnockoutBindingHandler;
412 options: KnockoutBindingHandler;
413 selectedOptions: KnockoutBindingHandler;
414 uniqueName: KnockoutBindingHandler;
415
416 // Rendering templates
417 template: KnockoutBindingHandler;
418
419 // Components (new for v3.2)
420 component: KnockoutBindingHandler;
421}
422
423interface KnockoutMemoization {
424 memoize(callback: Function): string;
425 unmemoize(memoId: string, callbackParams: any[]): boolean;
426 unmemoizeDomNodeAndDescendants(domNode: any, extraCallbackParamsArray: any[]): boolean;
427 parseMemoText(memoText: string): string;
428}
429
430interface KnockoutVirtualElement { }
431
432interface KnockoutVirtualElements {
433 allowedBindings: { [bindingName: string]: boolean; };
434 emptyNode(node: KnockoutVirtualElement): void;
435 firstChild(node: KnockoutVirtualElement): KnockoutVirtualElement;
436 insertAfter(container: KnockoutVirtualElement, nodeToInsert: Node, insertAfter: Node): void;
437 nextSibling(node: KnockoutVirtualElement): Node;
438 prepend(node: KnockoutVirtualElement, toInsert: Node): void;
439 setDomNodeChildren(node: KnockoutVirtualElement, newChildren: { length: number;[index: number]: Node; }): void;
440 childNodes(node: KnockoutVirtualElement): Node[];
441}
442
443interface KnockoutExtenders {
444 throttle(target: any, timeout: number): KnockoutComputed<any>;
445 notify(target: any, notifyWhen: string): any;
446
447 rateLimit(target: any, timeout: number): any;
448 rateLimit(target: any, options: { timeout: number; method?: string; }): any;
449
450 trackArrayChanges(target: any): any;
451}
452
453//
454// NOTE TO MAINTAINERS AND CONTRIBUTORS : pay attention to only include symbols that are
455// publicly exported in the minified version of ko, without that you can give the false
456// impression that some functions will be available in production builds.
457//
458interface KnockoutUtils {
459 //////////////////////////////////
460 // utils.domData.js
461 //////////////////////////////////
462
463 domData: {
464 get(node: Node, key: string): any;
465
466 set(node: Node, key: string, value: any): void;
467
468 getAll(node: Node, createIfNotFound: boolean): any;
469
470 clear(node: Node): boolean;
471 };
472
473 //////////////////////////////////
474 // utils.domNodeDisposal.js
475 //////////////////////////////////
476
477 domNodeDisposal: {
478 addDisposeCallback(node: Node, callback: Function): void;
479
480 removeDisposeCallback(node: Node, callback: Function): void;
481
482 cleanNode(node: Node): Node;
483
484 removeNode(node: Node): void;
485 };
486
487 addOrRemoveItem<T>(array: T[] | KnockoutObservable<T>, value: T, included: T): void;
488
489 arrayFilter<T>(array: T[], predicate: (item: T) => boolean): T[];
490
491 arrayFirst<T>(array: T[], predicate: (item: T) => boolean, predicateOwner?: any): T;
492
493 arrayForEach<T>(array: T[], action: (item: T, index: number) => void): void;
494
495 arrayGetDistinctValues<T>(array: T[]): T[];
496
497 arrayIndexOf<T>(array: T[], item: T): number;
498
499 arrayMap<T, U>(array: T[], mapping: (item: T) => U): U[];
500
501 arrayPushAll<T>(array: T[] | KnockoutObservableArray<T>, valuesToPush: T[]): T[];
502
503 arrayRemoveItem(array: any[], itemToRemove: any): void;
504
505 compareArrays<T>(a: T[], b: T[]): Array<KnockoutArrayChange<T>>;
506
507 extend(target: Object, source: Object): Object;
508
509 fieldsIncludedWithJsonPost: any[];
510
511 getFormFields(form: any, fieldName: string): any[];
512
513 objectForEach(obj: any, action: (key: any, value: any) => void): void;
514
515 parseHtmlFragment(html: string): any[];
516
517 parseJson(jsonString: string): any;
518
519 postJson(urlOrForm: any, data: any, options: any): void;
520
521 peekObservable<T>(value: KnockoutObservable<T> | T): T;
522
523 range(min: any, max: any): any;
524
525 registerEventHandler(element: any, eventType: any, handler: Function): void;
526
527 setHtml(node: Element, html: () => string): void;
528
529 setHtml(node: Element, html: string): void;
530
531 setTextContent(element: any, textContent: string | KnockoutObservable<string>): void;
532
533 stringifyJson(data: any, replacer?: Function, space?: string): string;
534
535 toggleDomNodeCssClass(node: any, className: string, shouldHaveClass: boolean): void;
536
537 triggerEvent(element: any, eventType: any): void;
538
539 unwrapObservable<T>(value: KnockoutObservable<T> | T): T;
540 unwrapObservable<T>(value: KnockoutObservableArray<T> | T[]): T[];
541
542 // NOT PART OF THE MINIFIED API SURFACE (ONLY IN knockout-{version}.debug.js) https://github.com/SteveSanderson/knockout/issues/670
543 // forceRefresh(node: any): void;
544 // ieVersion: number;
545 // isIe6: boolean;
546 // isIe7: boolean;
547 // jQueryHtmlParse(html: string): any[];
548 // makeArray(arrayLikeObject: any): any[];
549 // moveCleanedNodesToContainerElement(nodes: any[]): HTMLElement;
550 // replaceDomNodes(nodeToReplaceOrNodeArray: any, newNodesArray: any[]): void;
551 // setDomNodeChildren(domNode: any, childNodes: any[]): void;
552 // setElementName(element: any, name: string): void;
553 // setOptionNodeSelectionState(optionNode: any, isSelected: boolean): void;
554 // simpleHtmlParse(html: string): any[];
555 // stringStartsWith(str: string, startsWith: string): boolean;
556 // stringTokenize(str: string, delimiter: string): string[];
557 // stringTrim(str: string): string;
558 // tagNameLower(element: any): string;
559}
560
561interface KnockoutArrayChange<T> {
562 status: "added" | "deleted" | "retained";
563 value: T;
564 index: number;
565 moved?: number;
566}
567
568//////////////////////////////////
569// templateSources.js
570//////////////////////////////////
571
572interface KnockoutTemplateSourcesDomElement {
573 text(): any;
574 text(value: any): void;
575
576 data(key: string): any;
577 data(key: string, value: any): any;
578}
579
580interface KnockoutTemplateAnonymous extends KnockoutTemplateSourcesDomElement {
581 nodes(): any;
582 nodes(value: any): void;
583}
584
585interface KnockoutTemplateSources {
586
587 domElement: {
588 prototype: KnockoutTemplateSourcesDomElement
589 new(element: Element): KnockoutTemplateSourcesDomElement
590 };
591
592 anonymousTemplate: {
593 prototype: KnockoutTemplateAnonymous;
594 new(element: Element): KnockoutTemplateAnonymous;
595 };
596}
597
598//////////////////////////////////
599// nativeTemplateEngine.js
600//////////////////////////////////
601
602interface KnockoutNativeTemplateEngine extends KnockoutTemplateEngine {
603
604 renderTemplateSource(templateSource: Object, bindingContext?: KnockoutBindingContext, options?: Object): any[];
605}
606
607//////////////////////////////////
608// templateEngine.js
609//////////////////////////////////
610
611interface KnockoutTemplateEngine {
612
613 createJavaScriptEvaluatorBlock(script: string): string;
614
615 makeTemplateSource(template: any, templateDocument?: Document): any;
616
617 renderTemplate(template: any, bindingContext: KnockoutBindingContext, options: Object, templateDocument: Document): any;
618
619 isTemplateRewritten(template: any, templateDocument: Document): boolean;
620
621 rewriteTemplate(template: any, rewriterCallback: Function, templateDocument: Document): void;
622}
623
624//////////////////////////////////
625// tasks.js
626//////////////////////////////////
627
628interface KnockoutTasks {
629 scheduler: (callback: Function) => any;
630 schedule(task: Function): number;
631 cancel(handle: number): void;
632 runEarly(): void;
633}
634
635/////////////////////////////////
636interface KnockoutStatic {
637 utils: KnockoutUtils;
638 memoization: KnockoutMemoization;
639
640 bindingHandlers: KnockoutBindingHandlers;
641 getBindingHandler(handler: string): KnockoutBindingHandler;
642
643 virtualElements: KnockoutVirtualElements;
644 extenders: KnockoutExtenders;
645
646 applyBindings(viewModelOrBindingContext?: any, rootNode?: any): void;
647 applyBindingsToDescendants(viewModelOrBindingContext: any, rootNode: any): void;
648 applyBindingAccessorsToNode(node: Node, bindings: (bindingContext: KnockoutBindingContext, node: Node) => {}, bindingContext: KnockoutBindingContext): void;
649 applyBindingAccessorsToNode(node: Node, bindings: {}, bindingContext: KnockoutBindingContext): void;
650 applyBindingAccessorsToNode(node: Node, bindings: (bindingContext: KnockoutBindingContext, node: Node) => {}, viewModel: any): void;
651 applyBindingAccessorsToNode(node: Node, bindings: {}, viewModel: any): void;
652 applyBindingsToNode(node: Node, bindings: any, viewModelOrBindingContext?: any): any;
653
654 subscribable: KnockoutSubscribableStatic;
655 observable: KnockoutObservableStatic;
656
657 computed: KnockoutComputedStatic;
658 /**
659 * Creates a pure computed observable.
660 * @param evaluatorFunction Function that computes the observable value.
661 * @param context Defines the value of 'this' when evaluating the computed observable.
662 */
663 pureComputed<T>(evaluatorFunction: () => T, context?: any): KnockoutComputed<T>;
664 /**
665 * Creates a pure computed observable.
666 * @param options An object that defines the computed observable options and behavior.
667 * @param context Defines the value of 'this' when evaluating the computed observable.
668 */
669 pureComputed<T>(options: KnockoutComputedDefine<T>, context?: any): KnockoutComputed<T>;
670
671 observableArray: KnockoutObservableArrayStatic;
672
673 /**
674 * Evaluates if instance is a KnockoutSubscribable.
675 * @param instance Instance to be evaluated.
676 */
677 isSubscribable(instance: any): instance is KnockoutSubscribable<any>;
678 /**
679 * Clones object substituting each observable for it's underlying value. Uses browser JSON.stringify internally to stringify the result.
680 * @param viewModel Object with observables to be converted.
681 * @param replacer A Function or array of names that alters the behavior of the stringification process.
682 * @param space Used to insert white space into the output JSON string for readability purposes.
683 */
684 toJSON(viewModel: any, replacer?: Function | [string | number], space?: string | number): string;
685 /**
686 * Clones object substituting for each observable the current value of that observable.
687 * @param viewModel Object with observables to be converted.
688 */
689 toJS(viewModel: any): any;
690 /**
691 * Determine if argument is an observable. Returns true for observables, observable arrays, and all computed observables.
692 * @param instance Object to be checked.
693 */
694 isObservable(instance: any): instance is KnockoutObservable<any>;
695 /**
696 * Determine if argument is an observable. Returns true for observables, observable arrays, and all computed observables.
697 * @param instance Object to be checked.
698 */
699 isObservable<T>(instance: KnockoutObservable<T> | T): instance is KnockoutObservable<T>;
700 /**
701 * Determine if argument is a writable observable. Returns true for observables, observable arrays, and writable computed observables.
702 * @param instance Object to be checked.
703 */
704 isWriteableObservable(instance: any): instance is KnockoutObservable<any>;
705 /**
706 * Determine if argument is a writable observable. Returns true for observables, observable arrays, and writable computed observables.
707 * @param instance Object to be checked.
708 */
709 isWriteableObservable<T>(instance: KnockoutObservable<T> | T): instance is KnockoutObservable<T>;
710 /**
711 * Determine if argument is a computed observable.
712 * @param instance Object to be checked.
713 */
714 isComputed(instance: any): instance is KnockoutComputed<any>;
715 /**
716 * Determine if argument is a computed observable.
717 * @param instance Object to be checked.
718 */
719 isComputed<T>(instance: KnockoutObservable<T> | T): instance is KnockoutComputed<T>;
720
721 /**
722 * Returns the data that was available for binding against the element.
723 * @param node Html node that contains the binding context.
724 */
725 dataFor(node: Node): any;
726 /**
727 * Returns the entire binding context that was available to the DOM element.
728 * @param node Html node that contains the binding context.
729 */
730 contextFor(node: Node): any;
731 /**
732 * Removes a node from the DOM.
733 * @param node Node to be removed.
734 */
735 removeNode(node: Node): void;
736 /**
737 * Used internally by Knockout to clean up data/computeds that it created related to the element. It does not remove any event handlers added by bindings.
738 * @param node Node to be cleaned.
739 */
740 cleanNode(node: Node): Node;
741 renderTemplate(template: Function, viewModel: any, options?: any, target?: any, renderMode?: any): any;
742 renderTemplate(template: string, viewModel: any, options?: any, target?: any, renderMode?: any): any;
743 /**
744 * Returns the underlying value of the Knockout Observable or in case of plain js object, return the object. Use this to easily accept both observable and plain values.
745 * @param instance observable to be unwraped if it's an Observable.
746 */
747 unwrap<T>(instance: KnockoutObservable<T> | T): T;
748 /**
749 * Gets the array inside the KnockoutObservableArray.
750 * @param instance observable to be unwraped.
751 */
752 unwrap<T>(instance: KnockoutObservableArray<T> | T[]): T[];
753
754 /**
755 * Get information about the current computed property during the execution of a computed observables evaluator function.
756 */
757 computedContext: KnockoutComputedContext;
758
759 //////////////////////////////////
760 // templateSources.js
761 //////////////////////////////////
762
763 templateSources: KnockoutTemplateSources;
764
765 //////////////////////////////////
766 // templateEngine.js
767 //////////////////////////////////
768
769 templateEngine: {
770
771 prototype: KnockoutTemplateEngine;
772
773 new(): KnockoutTemplateEngine;
774 };
775
776 //////////////////////////////////
777 // templateRewriting.js
778 //////////////////////////////////
779
780 templateRewriting: {
781
782 ensureTemplateIsRewritten(template: Node, templateEngine: KnockoutTemplateEngine, templateDocument: Document): any;
783 ensureTemplateIsRewritten(template: string, templateEngine: KnockoutTemplateEngine, templateDocument: Document): any;
784
785 memoizeBindingAttributeSyntax(htmlString: string, templateEngine: KnockoutTemplateEngine): any;
786
787 applyMemoizedBindingsToNextSibling(bindings: any, nodeName: string): string;
788 };
789
790 //////////////////////////////////
791 // nativeTemplateEngine.js
792 //////////////////////////////////
793
794 nativeTemplateEngine: {
795
796 prototype: KnockoutNativeTemplateEngine;
797
798 new(): KnockoutNativeTemplateEngine;
799
800 instance: KnockoutNativeTemplateEngine;
801 };
802
803 //////////////////////////////////
804 // jqueryTmplTemplateEngine.js
805 //////////////////////////////////
806
807 jqueryTmplTemplateEngine: {
808
809 prototype: KnockoutTemplateEngine;
810
811 renderTemplateSource(templateSource: Object, bindingContext: KnockoutBindingContext, options: Object): Node[];
812
813 createJavaScriptEvaluatorBlock(script: string): string;
814
815 addTemplate(templateName: string, templateMarkup: string): void;
816 };
817
818 //////////////////////////////////
819 // templating.js
820 //////////////////////////////////
821
822 setTemplateEngine(templateEngine: KnockoutNativeTemplateEngine | undefined): void;
823
824 renderTemplate(template: Function, dataOrBindingContext: KnockoutBindingContext, options: Object, targetNodeOrNodeArray: Node, renderMode: string): any;
825 renderTemplate(template: any, dataOrBindingContext: KnockoutBindingContext, options: Object, targetNodeOrNodeArray: Node, renderMode: string): any;
826 renderTemplate(template: Function, dataOrBindingContext: any, options: Object, targetNodeOrNodeArray: Node, renderMode: string): any;
827 renderTemplate(template: any, dataOrBindingContext: any, options: Object, targetNodeOrNodeArray: Node, renderMode: string): any;
828 renderTemplate(template: Function, dataOrBindingContext: KnockoutBindingContext, options: Object, targetNodeOrNodeArray: Node[], renderMode: string): any;
829 renderTemplate(template: any, dataOrBindingContext: KnockoutBindingContext, options: Object, targetNodeOrNodeArray: Node[], renderMode: string): any;
830 renderTemplate(template: Function, dataOrBindingContext: any, options: Object, targetNodeOrNodeArray: Node[], renderMode: string): any;
831 renderTemplate(template: any, dataOrBindingContext: any, options: Object, targetNodeOrNodeArray: Node[], renderMode: string): any;
832
833 renderTemplateForEach(template: Function, arrayOrObservableArray: any[], options: Object, targetNode: Node, parentBindingContext: KnockoutBindingContext): any;
834 renderTemplateForEach(template: any, arrayOrObservableArray: any[], options: Object, targetNode: Node, parentBindingContext: KnockoutBindingContext): any;
835 renderTemplateForEach(template: Function, arrayOrObservableArray: KnockoutObservable<any>, options: Object, targetNode: Node, parentBindingContext: KnockoutBindingContext): any;
836 renderTemplateForEach(template: any, arrayOrObservableArray: KnockoutObservable<any>, options: Object, targetNode: Node, parentBindingContext: KnockoutBindingContext): any;
837
838 /**
839 * Executes a callback function inside a computed observable, without creating a dependecy between it and the observables inside the function.
840 * @param callback Function to be called.
841 * @param callbackTarget Defines the value of 'this' in the callback function.
842 * @param callbackArgs Arguments for the callback Function.
843 */
844 ignoreDependencies<T>(callback: () => T, callbackTarget?: any, callbackArgs?: any): T;
845
846 expressionRewriting: {
847 bindingRewriteValidators: any[];
848 twoWayBindings: any;
849 parseObjectLiteral: (objectLiteralString: string) => any[];
850
851 /**
852 Internal, private KO utility for updating model properties from within bindings
853 property: If the property being updated is (or might be) an observable, pass it here
854 If it turns out to be a writable observable, it will be written to directly
855 allBindings: An object with a get method to retrieve bindings in the current execution context.
856 This will be searched for a '_ko_property_writers' property in case you're writing to a non-observable
857 (See note below)
858 key: The key identifying the property to be written. Example: for { hasFocus: myValue }, write to 'myValue' by specifying the key 'hasFocus'
859 value: The value to be written
860 checkIfDifferent: If true, and if the property being written is a writable observable, the value will only be written if
861 it is !== existing value on that writable observable
862
863 Note that if you need to write to the viewModel without an observable property,
864 you need to set ko.expressionRewriting.twoWayBindings[key] = true; *before* the binding evaluation.
865 */
866 writeValueToProperty: (property: KnockoutObservable<any> | any, allBindings: KnockoutAllBindingsAccessor, key: string, value: any, checkIfDifferent?: boolean) => void;
867 };
868
869 /////////////////////////////////
870
871 bindingProvider: {
872 instance: KnockoutBindingProvider;
873 new(): KnockoutBindingProvider;
874 }
875
876 /////////////////////////////////
877 // selectExtensions.js
878 /////////////////////////////////
879
880 selectExtensions: {
881
882 readValue(element: HTMLElement): any;
883
884 writeValue(element: HTMLElement, value: any, allowUnset?: boolean): void;
885 };
886
887 components: KnockoutComponents;
888
889 /////////////////////////////////
890 // options.js
891 /////////////////////////////////
892
893 options: {
894 deferUpdates: boolean,
895
896 useOnlyNativeEvents: boolean
897 };
898
899 /////////////////////////////////
900 // tasks.js
901 /////////////////////////////////
902
903 tasks: KnockoutTasks;
904
905 /////////////////////////////////
906 // utils.js
907 /////////////////////////////////
908
909 onError?: (error: Error) => void;
910}
911
912interface KnockoutBindingProvider {
913 nodeHasBindings(node: Node): boolean;
914 getBindings(node: Node, bindingContext: KnockoutBindingContext): {};
915 getBindingAccessors?(node: Node, bindingContext: KnockoutBindingContext): { [key: string]: string; };
916}
917
918interface KnockoutComputedContext {
919 /**
920 * Returns the number of dependencies of the computed observable detected so far during the current evaluation.
921 */
922 getDependenciesCount(): number;
923 /**
924 * A function that returns true if called during the first ever evaluation of the current computed observable, or false otherwise.
925 * For pure computed observables, isInitial() is always undefined.
926 */
927 isInitial: () => boolean;
928 isSleeping: boolean;
929}
930
931//
932// refactored types into a namespace to reduce global pollution
933// and used Union Types to simplify overloads (requires TypeScript 1.4)
934//
935declare namespace KnockoutComponentTypes {
936 type ViewModel = ViewModelFunction | ViewModelSharedInstance | ViewModelFactoryFunction | AMDModule;
937
938 interface Config<T> {
939 viewModel?: T;
940 template: string | Node[] | DocumentFragment | TemplateElement | AMDModule;
941 synchronous?: boolean;
942 }
943
944 interface ComponentConfig<T = ViewModel> {
945 viewModel?: T;
946 template: any;
947 createViewModel?: any;
948 }
949
950 interface EmptyConfig {
951 }
952
953 // common AMD type
954 interface AMDModule {
955 require: string;
956 }
957
958 // viewmodel types
959 interface ViewModelFunction {
960 (params?: any): any;
961 }
962
963 interface ViewModelSharedInstance {
964 instance: any;
965 }
966
967 interface ViewModelFactoryFunction {
968 createViewModel: (params: any, componentInfo: ComponentInfo) => any;
969 }
970
971 interface ComponentInfo {
972 element: Node;
973 templateNodes: Node[];
974 }
975
976 interface TemplateElement {
977 element: string | Node;
978 }
979
980 interface Loader {
981 /**
982 * Define this if: you want to supply configurations programmatically based on names, e.g., to implement a naming convention.
983 * @see {@link https://knockoutjs.com/documentation/component-loaders.html}
984 */
985 getConfig?(componentName: string, callback: (result: ComponentConfig | null) => void): void;
986 /**
987 * Define this if: you want to take control over how component configurations are interpreted, e.g., if you do not want to use the standard 'viewModel/template' pair format.
988 * @see {@link https://knockoutjs.com/documentation/component-loaders.html}
989 */
990 loadComponent?(componentName: string, config: ComponentConfig, callback: (result: Definition | null) => void): void;
991 /**
992 * Define this if: you want to use custom logic to supply DOM nodes for a given template configuration (e.g., using an ajax request to fetch a template by URL).
993 * @see {@link https://knockoutjs.com/documentation/component-loaders.html}
994 */
995 loadTemplate?(componentName: string, templateConfig: any, callback: (result: Node[] | null) => void): void;
996 /**
997 * Define this if: you want to use custom logic to supply a viewmodel factory for a given viewmodel configuration (e.g., integrating with a third-party module loader or dependency injection system).
998 * @see {@link https://knockoutjs.com/documentation/component-loaders.html}
999 */
1000 loadViewModel?(componentName: string, viewModelConfig: any, callback: (result: any) => void): void;
1001 suppressLoaderExceptions?: boolean;
1002 }
1003
1004 interface Definition {
1005 template: Node[];
1006 createViewModel?(params: any, options: { element: Node; }): any;
1007 }
1008}
1009
1010interface KnockoutComponents {
1011
1012 /**
1013 * Registers a component, in the default component loader, to be used by name in the component binding.
1014 * @param componentName Component name. Will be used for your custom HTML tag name.
1015 * @param config Component configuration.
1016 */
1017 register<T = KnockoutComponentTypes.ViewModel>(componentName: string, config: KnockoutComponentTypes.Config<T> | KnockoutComponentTypes.EmptyConfig): void;
1018 /**
1019 * Determine if a component with the specified name is already registered in the default component loader.
1020 * @param componentName Component name.
1021 */
1022 isRegistered(componentName: string): boolean;
1023 /**
1024 * Removes the named component from the default component loader registry. Or if no such component was registered, does nothing.
1025 * @param componentName Component name.
1026 */
1027 unregister(componentName: string): void;
1028 /**
1029 * Searchs each registered component loader by component name, and returns the viewmodel/template declaration via callback parameter.
1030 * @param componentName Component name.
1031 * @param callback Function to be called with the viewmodel/template declaration parameter.
1032 */
1033 get(componentName: string, callback: (definition: KnockoutComponentTypes.Definition) => void): void;
1034 /**
1035 * Clears the cache knockout creates to speed up component loading, for a given component.
1036 * @param componentName Component name.
1037 */
1038 clearCachedDefinition(componentName: string): void
1039 defaultLoader: KnockoutComponentTypes.Loader;
1040 loaders: KnockoutComponentTypes.Loader[];
1041 /**
1042 * Returns the registered component name for a HTML element. Can be overwriten to to control dynamically which HTML element map to which component name.
1043 * @param node html element that corresponds to a custom component.
1044 */
1045 getComponentNameForNode(node: Node): string;
1046}
1047
1048declare var ko: KnockoutStatic;
1049
1050declare module "knockout" {
1051 export = ko;
1052}
1053
\No newline at end of file