1

I have created the following:

module Admin.Grid {
    export function addGridControls() {

        $('#createLink')
            .click(function () {
                var $link = $(this);
                $link.prop('disabled', true);
                adminDialog($link);
                return false;
            });
    }
}

This is converted to:

var Admin;
(function (Admin) {
    (function (Grid) {
        function addGridControls() {
            $('#createLink').click(function () {
                var $link = $(this);
                $link.prop('disabled', true);
                adminDialog($link);
                return false;
            });

Previously when it was not inside a module I called the function like this:

$(document).ready(function () {
    "use strict";
    addGridControls()
});

Now it's inside of a module what's the best way for me to call this function so it gets executed every time the document is ready?

4

2 回答 2

1

这样做的一种方法是将函数添加到某个对象。

var Admin = {};
(function (Admin) {
    (function (Grid) {
        Admin.addGridControls = function () {
....

并称它为

$(document).ready(function () {
    "use strict";
    Admin.addGridControls()
});
于 2012-11-14T06:09:41.330 回答
0

正如@Mike Lin 评论的那样,您需要导入模块。

在 TypeScript 中工作(并假设 AMD 模块格式,您的模块在另一个文件中),您可以这样做:

import g = module('path-to-admin-grid-module');

$(document).ready(() => {
    "use strict";
    g.Admin.Grid.addGridControls();
});

否则,如果您只是在同一个文件中使用内部模块,则很简单:

$(document).ready(() => {
        "use strict";
        Admin.Grid.addGridControls();
});

后一种情况在演练:模块示例中很好地预览:http ://www.typescriptlang.org/Playground/

这里有一个很好的前者示例:TypeScript compile AMD modules with required definitions和 AMD 在这里更详细地介绍:http ://requirejs.org/docs/whyamd.html

于 2012-11-14T11:53:58.887 回答