0

I have a skeleton of a module which adds a record to a database with a button click.

var Person = (function () {
    var ajaxOpts = {
        type: "POST",
        url: "",
        contentType: "application/json",
        dataType: "json",
        success: function () { },
        error: function () { },
        data: {}
    }
    function insert(data) {
        ajaxOpts.url = "../Service.asmx/InsertPerson";
        ajaxOpts.data = JSON.stringify(data);
        ajaxOpts.error = function (xhr) {
            console.log(xhr.status);

        };
        ajaxOpts.success = function (data) {
            console.log('record successfully added');
            console.log(data.d);

        }
        $.ajax(ajaxOpts);
    };
    return {
        insert: insert
    }
} ());

and I call it from my webpage like:

$(document).ready(function () {
            $('#btnSubmit').click(function () {
                var data = {
                    personId: $('#personId').val(),
                    firstName: $('#firstName').val(),
                    lastName: $('#lastName').val()
                };
                Person.insert(data);
            });
        });

How can I modify this code to make sure that $ is the jQuery object and not another library?

2
  • In both files or where exactly? Commented Nov 15, 2013 at 2:03
  • Let's say both files are loaded with a CDN. Commented Nov 15, 2013 at 2:06

2 Answers 2

1

For plugins you usually wrap the code in an IIFE and map jQuery to $. You could do the same for your module (you even already have an IIFE):

var Person = (function($) {
    // ...
}(jQuery));

For document.ready callbacks, a reference to jQuery is passed to the callback:

jQuery(document).ready(function($) {
    // ...
});

Just make sure that you are using jQuery outside of the functions.

Sign up to request clarification or add additional context in comments.

4 Comments

So all I really need to do is change my IIFE like you have it their in your first code block? What makes that work? I think it takes whatever parameters is passed to it and returns jQuery? I guess this works because I'm calling Person methods inside of jQuery?
"What makes that work?" I'm not sure what you mean. You pass jQuery as argument to the function and name the parameter $. So any use of $ inside the function refers to the parameter and not to the global $. Is this what you wanted to know?
I worded that poorly, let's say I was using another library that used $ besides jQuery, how do those objects not collide and mess up (doesn't prototype use $ as well?)
Well, inside the function, $ will refer to jQuery then. It will shadow the global $ variable. If you also want to be able to access the other library, you could use $j instead of $ to access jQuery and $ to access the other library.
0

How about replacing code in your main page like this:

(function($) {
    $('#btnSubmit').click(function () {
        var data = {
            personId: $('#personId').val(),
            firstName: $('#firstName').val(),
            lastName: $('#lastName').val()
        };
        Person.insert(data);
    });
}(jQuery));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.