我可以覆盖typescript中的document.ready

时间:2013-03-13 16:58:15

标签: jquery onload typescript document-ready onload-event

我有一个具有不同页面的应用程序,并且大致每个页面都有自己的js(由typescript编译器生成)。每个页面的document.ready方法都有一些共享属性,我希望将共享函数/属性放入基本的typescript类中。

所以看起来应该是

 class PageX extends BasePage
    {
        onLoad => which should act like document.ready
    }

这可以吗?我不确定这是否可能?

2 个答案:

答案 0 :(得分:4)

如果您想在每个页面中使用基本功能,那么您需要在每个页面中包含基本脚本。派生类不会将父功能复制到子项中。

您可以在.ts文件中包含一个包含公共行为和属性的PageBase类,然后从每个页面特定行为派生一个类。如下所示:

// Base.ts
class PageBase {
  commonProperty: any;

  constructor(doc:HTMLDocument){
    doc.onload = function(){
      alert('Common code here');
    }
  }
}

上面的编译输出(base.js)将通过每个页面上的脚本标记包含在内,然后是每页生成的脚本,如...

// pageX.ts

/// <reference path="Base.ts"/>
class PageX extends PageBase {
  constructor(){
    super(window.document);
    // Rest of my code here...
  }
}

这听起来是关于你所追求的吗?

答案 1 :(得分:1)

我认为逻辑不属于一个类。

但是如果你在.ts文件的顶部声明一个函数var并从你的外部doc.ready事件中调用它,那么你可以在类实例中监听该函数的变化。

Function listener in javascript and/or jQuery

Listen to state and function invocations

相关问题