Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery mobile listview - check initialization is complete

I've coded my way into a corner.

I need to call listview("refresh") on a list, however it may not have been initialized at the point I am calling the refresh method.

Is there a way to check if the component has initialized or not?

This is the error I get:

cannot call methods on listview prior to initialization

like image 313
Chin Avatar asked Feb 29 '12 03:02

Chin


2 Answers

When a listview widget is initialized, it's given the ui-listview class, so we can test for this class to see if it has been initialized:

//select the listview
var $myUL = $('#my-ul-element');

//add a list-item to the listview
$myUL.append('<li>I\'m New!</li>');

//check if the listview has the ui-listview class
if ($myUL.hasClass('ui-listview')) {

    //this listview has already been initialized, so refresh it
    $myUL.listview('refresh');
} else {

    //this listview has not yet been initialized, so it gets initialized
    $myUL.listview();//or you can use .trigger('create');
}

This should help alleviate the error you're getting.

Also, the .hasClass('[class]') function returns true/false if the element has the [class]: http://api.jquery.com/hasClass

like image 107
Jasper Avatar answered Oct 21 '22 09:10

Jasper


Try

$list.listview();
$list.listview('refresh');
like image 4
bmurmistro Avatar answered Oct 21 '22 07:10

bmurmistro