2012-10-22 124 views
4

我有以下指向backbone.d.ts定义。TypeScript,RequireJS垫片,环境模块声明

import Backbone = module("../../../dep/backbone/backbone"); 

export class Codebook extends Backbone.Model { 

    defaults() { 
     return { 
      id: -1, 
      title: -1 
     } 
    } 

    initialize() { 
     // do nothing 
    } 

} 

--module amd它产生以下。

define(["require", "exports", "../../../dep/backbone/backbone"], function(require, exports, __Backbone__) { 

因为全局的缘故,我已经用我的RequireJS配置文件擦过骨干。我想我的定义只是说backbone而不是相对路径。除了后期构建过程以外,是否还有任何解决方法来操作定义?

回答

3

我认为这个问题的解决方案是把环境声明放在最终的真实模块的相同位置,所以你可以用相同的路径引用它。

所以backbone.d.ts需要在相同的位置backbone.js

+0

它在相同的位置,但垫片照顾我的出口和依赖关系,而无需使真正的backbone.js成为amd-ified模块。 – ryan

1

而不是使用相对路径,我总是使用根路径。像这样的:

import underscore = module('libs/underscore/underscore'); 

和黑客是你可以在你的垫片中定义相同的名称。事情是这样的:

require.config({ 
    paths: { 
     'libs/underscore/underscore': 'libs/underscore/underscore' 
    }, 
    shim: { 
     'libs/underscore/underscore': { 
      exports: '_' 
     } 
    } 
}); 

通过使用总是从根目录的路径,该路径保持,如果你是使用相对路径,而不是相同的。这是一个黑客攻击,但它适用于我(GitHub)。

0

我刚刚在how to use AMD modules in TypeScript上放了个博客。

首先,只使用一个参考路径,以骨干如下:

/// <reference path="../../modules/Backbone.d.ts" /> 

然后定义你的类没有进口:

export class MTodoCollectionView extends Backbone.View { 
... 
} 

然后你require.config,并APPLOADER:

require.config({ 
    baseUrl: '../', 
    paths: { 
     'underscore': 'lib/underscore', 
     'backbone': 'lib/backbone' 
    }, 
    shim: { 
     underscore: { 
      exports: '_' 
     }, 
     backbone: { 
      deps: ["underscore", "jquery"], 
      exports: "Backbone" 
     } 
    } 
}); 

require(['jquery','underscore','backbone','console','app/AppMain', 
    ], 
    ($, _, Backbone, console, main) => { 
    // code from window.onload 
    var appMain = new main.AppMain(); 
    appMain.run(); 
}); 

一旦应用匹配的代码的需要块,主链是全局定义。
玩得开心,

相关问题