3

When i run my application i get: "Uncaught TypeError: Cannot read property 'init' of undefined" The alert in the document reasdy function runs, so i know jQuery is loaded at that point. But i don't know why Task is undefined.

This is in a MVC5 application, and running jQuery 1.10.2

Please help, jsFiddle works fine, and JsHint shows no errors.

    var Task = (function () {

    var initialize = function () {
        console.log($);
        bindEvents();
    };
    var postFilter = function () {
        console.log("clicked");
        var postData = {
            UserID: $('#user_select').val,
            TaskTypeID: $('#taskTypeSelect').val,
            TaskStatusID: $('#status_select').val,
            PriorityID: $('#priority_select').val
        };

        $.ajax({
            url: "/Tasks/_AllTaskView",
            data: postData,
            success: function (response) {
                $('#results').html(response);
            }

        });
    };

    var bindEvents = function () {
        $('#submit_filter').on('click', postFilter);
    };

    return
    {
        init : initialize

    };

})();

$(document).ready(function () {
    alert()
    Task.init();
});
1
  • I am upvoting simply because I had no idea that return does not allow its value starting on subsequent lines (something every JS coder should be made aware of) :) Commented Dec 11, 2014 at 14:27

2 Answers 2

6

Put the curly brace of your return on the same line as return.

Instead of:

return
{
    init : initialize

};

Do this:

return {
    init : initialize
};

JavaScript has automatic semi-colon insertion which will automatically end the return statement before it hits the curly brace.

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

2 Comments

My bad (stupid inconsistent language.. give me C# any day ) - comment removed. Hey, it encouraged 3 other upvotes... don't complain :)
As a completely useless side note, this one works too: jsfiddle.net/3s6n4d5y/2
0

Here try this:

return{ init : function(){
                 initialize();
               }
      }

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.