0

All,

My code was failing JSLint for the "use strict" issue. As suggested here : Disable "use the function form of use strict" but keep the "Missing 'use strict' statement" warning

If you wrap all the functions in the javascript file with another function wrapper, it will solve this issue.

However now after doing this all my functions are undefined when I call them from the page?

(function () {
"use strict";

/**
 * Update the page with a message informing the user
 * an email has been sent to their email address
 * @param details user details (email address)
 */
function shareEmailSent(details) {

    // unhide confirmation message
    $('strong#emailAddress').text(details.email);
    $('#confirmationMessage').removeClass('hide');
}

/**
 * Handle error
 */
function showError() {
    return false;
}

/**
 * Makes a POST request to send the current user
 * an email containing their unqiue share url
 * @param emailList
 */
function sendEmail(emailList) {
    var data = {};
    data.formToken = $('#formToken').val();
    data.emails = emailList;
    $.mooAjax({
        url: "/ajax/share/refer_a_friend_email.php",
        type: "POST",
        data: data,
        dataType: "json",
        error: showError,
        success: function (response) {
            shareEmailSent(response);
        }
    });
}
}());

e.g.

shareEmailSent is not defined

How can I fix this but also pass the JSLint issue as well?

Thanks

Community
  • 1
  • 1
tomaytotomato
  • 3,788
  • 16
  • 64
  • 119

1 Answers1

1

There are several approaches you can take. In descending order of niceness:

  1. Don't call the functions from outside. Bind all your event handlers using JavaScript (e.g. with addEventListener)
  2. Use the revealing module pattern
  3. Explicitly make functions globals (e.g. with window.someFunction = someFunction).

Obviously, since sendEmail calls the other functions itself, it is probably the only one that would need to be exposed if you used the second or third approach.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Thanks, for the help. I went with option 3. However now it breaks my functions validation for some reason, any idea why assigning a function to a global variable would affect the logic? – tomaytotomato Dec 10 '15 at 10:56
  • It shouldn't do, and I've no idea what you mean by "breaks my functions validation". – Quentin Dec 10 '15 at 11:02