Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
744 views
in Technique[技术] by (71.8m points)

knockout.js - How do I define a knockout binding handler in typescript?

I normally add custom knockout binding handlers in JavaScript via

ko.bindingHandlers.myBindingHandler = {...}

but now I have to add them in TypeScript via

ko.bindingHandlers["myBindingHandler"] = {...}

otherwise I get this error because I'm using typescript.d.ts:

The property 'myBindingHandler' does not exist on value of type 'KnockoutBindingHandlers'

I don't like the ["property"] approach because then I can't reference it or get intellisense on it later.

So, how can I add my custom binding handler to knockout while using definitelyTyped's knockout definition, while also being able to reference my definition via intellisense, etc?

question from:https://stackoverflow.com/questions/16204142/how-do-i-define-a-knockout-binding-handler-in-typescript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Defining a custom binding handler

Its actually pretty easy, just add it (myBindingHandler) to the KnockoutBindingHandlers interface right before you define your custom binding handler. Please note that you have to do make this addition to the interface, within a .d.ts file as of version 1.0 (or possibly earlier).

bindingHandlers.d.ts

/// <reference path="typings/knockout/knockout.d.ts" />

interface KnockoutBindingHandlers {
    myBindingHandler: KnockoutBindingHandler;
}

myBindingHandler.ts

/// <reference path="bindingHandler.d.ts" />

ko.bindingHandlers.myBindingHandler = {...}

Now everything works. This will not overwrite any existing definitions or declarations, so your definition will sit along side of ko.bindingHandlers.text, etc.

Just be careful, because if you do not include an actual definition of myBindingHandler and you reference it elsewhere, it will compile due to the definition you added to KnockoutBindingHandlers, but it will break at runtime because there is no implementation of myBindingHandler.

The documentation for adding custom bindinghandlers in knockoutjs is here

Using fn to add custom functions with TypeScript

Similarly, to add something to ko.observable.fn, you'd do this in typescript

interface KnockoutObservableFunctions  { 
    myFnExtension(args: any): returnType; 
}

and call it with

// x will be defined as a returnType automatically, but you could specify it if you like, either way
var x: returnType = ko.observable("value").myFnExtension(args);

Note: There are different interfaces for the subscribable, observable, observableArray, and computed types:

  • ko.subscribable.fn ... add to KnockoutSubscribableFunctions
  • ko.observable.fn ... add to KnockoutObservableFunctions
  • ko.observableArray.fn ... add to KnockoutObservableArrayFunctions
  • ko.computed.fn ... add to KnockoutComputedFunctions

The documentation for adding onto fn in knockoutjs is here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...