2013-07-06 86 views
0

我在打字稿中创建了一个小型的库,我希望能够与我的很多项目一起使用,其中一些项目使用requirejs,而其他项目则不使用。使用打字稿制作AMD可选

我见过其他脚本这样做,它使用define并检查AMD,如果它们不在那里,它将对象附加到窗口对象或其他东西。

我在想什么是做这件事的最好方法?如果可能的话,任何快捷方式或w/e在打字稿。

下面是一个例子模块

export module Utilities { 
//This is used to grab query string values from a javascript file 
//pass the filename (without .js) into the constructor, then use 
//GetValue(name) to find the query string values 
export class QueryStringHelper { 
    names: string[] = []; 
    values: string[] = []; 
    constructor(public fileName: string) { 
     this.getQueryStringNameAndValues(); 
    } 
    // GetValue(queryStringName: string) => number; 

    GetValue(queryStringName: string) { 
     var i = this.names.indexOf(queryStringName); 
     if (i == -1) 
      return undefined; 
     else { 
      if (this.values.length > i) 
       return this.values[i]; 
     } 
    } 
    getQueryStringNameAndValues() { 
     var doc: HTMLDocument = document; 
     var scriptQuery: string = ''; 

     // Look for the <script> node that loads this script to get its parameters. 
     // This starts looking at the end instead of just considering the last 
     // because deferred and async scripts run out of order. 
     // If the script is loaded twice, then this will run in reverse order. 
     // take from Google Prettify 
     for (var scripts = doc.scripts, i = scripts.length; --i >= 0;) { 
      var script = <HTMLScriptElement>scripts[i]; 
      var match = script.src.match("^[^?#]*\\/" + this.fileName + "\\.js(\\?[^#]*)?(?:#.*)?$"); 
      if (match) { 
       scriptQuery = match[1] || ''; 
       // Remove the script from the DOM so that multiple runs at least run 
       // multiple times even if parameter sets are interpreted in reverse 
       // order. 
       script.parentNode.removeChild(script); 
       break; 
      } 
     } 

     var that = this; 
     scriptQuery.replace(
      /[?&]([^&=]+)=([^&]+)/g, 
      function (_, name, value) { 
       value = decodeURIComponent(value); 
       name = decodeURIComponent(name); 
       that.names.push(name); 
       that.values.push(value); 
       return ""; 
      }); 
     } 
    } 
} 

当然这作品时,我使用requireJS,以及它是如何解释在网站上加载它,但如果我尝试加载此不requireJS,我得到了明显define is not defined例外。

我该如何去做......“可选”。

我敢肯定,这之前已经回答了,但我想不出该怎么寻找(并有麻烦命名自由编辑question..feel)

编辑示例

所以我知道这种方式是有效的,但这是一种正确的方式来做我想找的?如果我这样做,我将来会遇到什么问题?

//i guess by default this is attached to the window object 
//so it's automatically available to anything that just includes it 
class TestClass { 
    constructor(public testValue: string) { 
    } 

    getTestValue(): string { 
    return this.testValue; 
    } 
} 
//attempt to support amd 
if (typeof window['define'] === "function" && window['define']['amd']) { 
    window['define']("TestClass", [], function() { 
     return TestClass; 
    }); 
} 

和产生此JavaScript

var TestClass = (function() { 
    function TestClass(testValue) { 
     this.testValue = testValue; 
    } 
    TestClass.prototype.getTestValue = function() { 
     return this.testValue; 
    }; 
    return TestClass; 
})(); 

if (typeof window['define'] === "function" && window['define']['amd']) { 
    window['define']("TestClass", [], function() { 
     return TestClass; 
    }); 
} 

回答

1

这是不是一个图案打字稿支持。原因是这种技术只适用于本身没有任何模块依赖关系的模块。您仍然可以使用TypeScript作为模块,但您必须自己拨打define而不是使用顶级export关键字。

+0

感谢您的回答,我编辑我的帖子到创建模块的位置,并自己添加了定义,并取出了导出和顶层模块。这是你在说什么吗? –