4

i have this js and qunit test below. why the browser gave me the listClasses is not defined? How to solve it. I saw mostly did

function ajax() {  
$.ajax({   
});}

but if i did like below how to do the test?

$('#MregisteredClasses').on('pageinit', function listClasses(){
var rowInput = "1";
var pageInput = "1";

$.ajax({
    url: 'http://137.57.102.146:8080/Training/getRegisteredClassesData.html',
    data: ( {rows : rowInput , page : pageInput}),
    type: 'POST',

    success: function(json_results){
    $('#list').append('<ul data-role="listview" data-inset="true"</ul>');
        listItems = $('#list').find('ul');
        $.each(json_results.rows, function(key) {
            html = "<li data-mini='true' id='icon'><a href='http://137.57.102.146:8080/Training/MRegisteredClassesDetail.phone?courseId=" 
                   + [json_results.rows[key].courseId] + "&regNo=" + [json_results.rows[key].regNo] +
                   "' rel='external'>" + json_results.rows[key].courseName+ "</a>"
                   + "<a href='http://137.57.102.146:8080/Training/MRateCourse.phone?courseId=" 
                   + [json_results.rows[key].courseId] + "&regNo=" + [json_results.rows[key].regNo] + 
                   "' rel='external'>RATE THIS COURSE</a></li>" ; 
            listItems.append(html); 
        });

        $('#list ul').listview(); 
    },
});
});

and this is qunit test

 test('asynchronous test', function() {  
// Pause the test, and fail it if start() isn't called after one second  
stop();  

listClasses(function() {  
    // ...asynchronous assertions  
    ok(true, "Success"); 
});  

setTimeout(function() {  
    start();  
}, 2000);  
});  

2 Answers 2

2

There's a difference between function declaration and named function expression.

The declaration is of the form:

function myFunc() {
    /* body */
}

Expressions are everything else:

var x = function myFunc() {
    /* body */
};

// or

alert(function myFunc() {
    / * body */
});

When a function expression has a name after the function keyword, the function is accessible by this name only inside the function body. Hence your problem.

var x = function myFunc() {
    /* body */
};

x(); // okay
myFunc(); // reference error

More info in Named function expressions demystified.

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

Comments

0

$('#MregisteredClasses').on('pageinit', function listClasses(){ ... is incorrect usage.

The function either needs to be anonymous (i.e. drop the listClasses bit) in which case running listClasses() will fail.

What you need to do is externalise the function declaration.

i.e.:

function listClasses(){
  var rowInput = "1";
  var pageInput = "1";

  $.ajax({
    url: 'http://137.57.102.146:8080/Training/getRegisteredClassesData.html',
    data: ( {rows : rowInput , page : pageInput}),
    type: 'POST',

    success: function(json_results){
    $('#list').append('<ul data-role="listview" data-inset="true"</ul>');
        listItems = $('#list').find('ul');
        $.each(json_results.rows, function(key) {
            html = "<li data-mini='true' id='icon'><a href='http://137.57.102.146:8080/Training/MRegisteredClassesDetail.phone?courseId=" 
                   + [json_results.rows[key].courseId] + "&regNo=" + [json_results.rows[key].regNo] +
                   "' rel='external'>" + json_results.rows[key].courseName+ "</a>"
                   + "<a href='http://137.57.102.146:8080/Training/MRateCourse.phone?courseId=" 
                   + [json_results.rows[key].courseId] + "&regNo=" + [json_results.rows[key].regNo] + 
                   "' rel='external'>RATE THIS COURSE</a></li>" ; 
            listItems.append(html); 
        });

        $('#list ul').listview(); 
    }
});
}

Then call the function from the on command:

$('#MregisteredClasses').on('pageinit', listClasses)

Assuming your script only failed on listClasses(), then I would assume that the listClasses name is just dropped silently.

7 Comments

Joe bowman : i try ur answer than it display this error Uncaught TypeError: Object [object Object] has no method 'listview' Source: 137.57.102.146:8080/Training/js/MRegisteredClasses.js:23
As the error says, the object returned by $('#list ul') has no listview() function. Where is listview defined?
$('#list ul').listview(); is this not define the listview?
nope - that's calling the listview() function. The function must be defined elsewhere, in order to be called. Do you need to include another script file somewhere that includes the listview function?
i don't understand. the listview is the method for jquerymobile. am i right?
|

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.