Jquery mobile listview - check initialization is complete

我与影子孤独终老i 提交于 2019-12-17 20:43:25

问题


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


回答1:


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




回答2:


Try

$list.listview();
$list.listview('refresh');



回答3:


For me the solution to this message was to add data-role="listview" to my ul element. I could then use the method .listview('refresh') in my event handlers.




回答4:


you can simply create a global variable

var is_mobile_init = false;

at the top of your scripts. then set it to true after you include jquery mobile.

then before refreshing write,

if (is_mobile_init)
    $myUL.listview('refresh');

this way, you don't need to keep track of all the various classes and it is intuitive



来源:https://stackoverflow.com/questions/9493215/jquery-mobile-listview-check-initialization-is-complete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!