Samantha J T Star
Samantha J T Star

Reputation: 32808

How can I call a function that's created inside a chain of other functions?

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?

Upvotes: 1

Views: 99

Answers (2)

Jude Fisher
Jude Fisher

Reputation: 11294

As @Mike Lin has commented, you need to import the module.

Working in TypeScript (and assuming AMD module format, with your module in another file), you can do it like this:

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

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

Otherwise, if you're just using internal modules within the same file, it's as simple as:

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

The latter case is nicely previewed in the Walkthrough: Modules example here: http://www.typescriptlang.org/Playground/

There's a pretty good example of the former here: TypeScript compile AMD modules with required defines and AMD is covered in more detail here: http://requirejs.org/docs/whyamd.html

Upvotes: 0

Pranav 웃
Pranav 웃

Reputation: 8477

One way of doing this is, is to add the function to some object.

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

And call it like

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

Upvotes: 1

Related Questions