如何在打字稿中定义敲除绑定处理程序? [英] How do I define a knockout binding handler in typescript?

查看:91
本文介绍了如何在打字稿中定义敲除绑定处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常会添加自定义 淘汰赛

I normally add custom knockout binding handlers in JavaScript via

ko.bindingHandlers.myBindingHandler = {...}

但是现在我必须通过

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

否则,我会收到此错误,因为我正在使用 typescript.d.ts :

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

类型'KnockoutBindingHandlers'的值上不存在属性'myBindingHandler'

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

我不喜欢["property"]方法,因为那样我以后就无法引用它或获得智能感知.

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

因此,如何在使用 definitelyTyped的基因剔除时将我的自定义绑定处理程序添加到基因剔除中定义 ,同时还能通过智能感知等引用我的定义?

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?

推荐答案

定义自定义绑定处理程序

实际上非常简单,只需在定义自定义绑定处理程序之前将它(myBindingHandler)添加到KnockoutBindingHandlers接口 . 请注意,您必须在1.0版(或更早版本)的.d.ts文件中对界面进行此添加.

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 = {...}

现在一切正常.这不会覆盖任何现有的定义或声明,因此您的定义将位于ko.bindingHandlers.text等的旁边.

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

请小心,因为如果不包括myBindingHandler的实际定义,而在其他地方引用它,则由于您添加到KnockoutBindingHandlers的定义而将编译它,但由于没有定义,它会在运行时中断myBindingHandler的实现.

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

类似地,要在ko.observable.fn中添加一些内容,您可以在打字稿中进行

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

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

并用

// 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);

注意:subscribableobservableobservableArraycomputed类型有不同的接口:

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

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

用于在剔除js中添加到fn的文档是 此处

The documentation for adding onto fn in knockoutjs is here

这篇关于如何在打字稿中定义敲除绑定处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆