Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Alert: Stupid Question

I have task.js file, with a class called Task in it, using the revealing modular pattern.

i'm working on a MVC application, and i've checked the HTML Jquery library gets loaded before my JS script, the following code for the contents of my JS file is below.

the file shows as loaded in the HTML (last), and Network:Scripts in developer tools.

The app works if i use the literal pattern for the class.
Why will it not work with the module pattern? I've used this pattern all the time and not had this problem, what am i doing wrong?

Error:

"Uncaught TypeError: Cannot read property 'init' of undefined"

JavaScript

C#
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: S('#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();
});
Posted
Updated 11-Dec-14 1:25am
v2

1 solution

You're going to scream when I tell you.

It's bad line breaking.

At the end of the anonymous function that creates Task you say;

JavaScript
return //that's it, javascript will return and not read the next statement
{
    init: initialize


All you have to do is make it clear that more is to come by placing the opening bracked after the return on the same line;

JavaScript
return { 
 init: initialize
.....


If you use a helper like jshint/jslint this will be caught by them and they will point it out.

Happy coding
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900