Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Mobile rendering problems with content being added after the page is initialized

I'm using jQuery Mobile and Backbone JS for a project. It's mostly working, using jQuery Mobile's event 'pagebeforeshow' to trigger the correct Backbone View. In the Backbone View for that particular jQuery Mobile page, that's where it's doing all the dynamic things needed. Some of the things the views do is pull in certain bits using Underscore's templating system.

This is all great until where I pulling in form bits using the templating system. For example, a set of dynamic radio buttons (which are generated from a Backbone Collection). These radio buttons I want to style up using what jQuery Mobile has to offer. At the moment, jQuery Mobile is not picking up these dynamically injected radio buttons. I solved this issue previously when doing sliders by just calling the jQuery Mobile widget "slider()" method again and it seemed to refresh them... This doesn't seem to be the case with these radio buttons.

In the Backbone View, I tried calling the widget methods again:

$(this.el).find("input[type='radio']").checkboxradio();
$(this.el).find(":jqmData(role='controlgroup')").controlgroup();

I tried them the other way around too, but it seemed I need to do it this way for the grouping styling to work etc. But this just doesn't seem right! ...doing this also caused errors when I clicked on the radio buttons, saying: "cannot call methods on checkboxradio prior to initialization; attempted to call method 'refresh'"?

It seems there should be a way in jQuery Mobile to re-initialize the page or something?! I noticed there is a 'page' widget in the source code.

How does jQuery Mobile handle forms/elements being injected into the DOM after the page is made? Is there a clean way of handling how it makes up the forms? There must be a clean way of calling on the forms to render 'the jQuery Mobile way' without it just relying on data attribute tags in the base HTML?

Any help or insight into this problem would be greatly appreciated... I'm very much on this quest of trying to get Backbone JS and jQuery Mobile to work nicely together.

Many thanks, James

like image 928
littlejim84 Avatar asked Apr 13 '11 15:04

littlejim84


3 Answers

update

Since jQueryMobile beta2 there is an event to do this. .trigger('create') on an element to cause everything inside it to be painted correctly.

Another question that is not a duplicate, but requires an answet I posted over 10 times :)

[old answer]

try .page()

More details in my faq: http://jquerymobiledictionary.pl/faq.html

like image 75
naugtur Avatar answered Oct 22 '22 11:10

naugtur


Refreshing the whole page worked for me:

$('#pageId').page('destroy').page();
like image 23
Jaime Botero Avatar answered Oct 22 '22 12:10

Jaime Botero


I'm not sure if this helps but when adding dynamic elements I was using .page() in the sucess ajax call itself (example here and here) but I found that it was not working as expected. I found that in the ajax call it's better to refresh the element (if it's a form element) to use these documented methods:

  • Checkboxes:

    $("input[type='checkbox']").attr("checked",true).checkboxradio("refresh");
    
  • Radios:

    $("input[type='radio']").attr("checked",true).checkboxradio("refresh");
    
  • Selects:

    var myselect = $("select#foo");
    myselect[0].selectedIndex = 3;
    myselect.selectmenu("refresh");
    
  • Sliders:

    $("input[type=range]").val(60).slider("refresh");
    
  • Flip switches (they use slider):

    var myswitch = $("select#bar");
    myswitch[0].selectedIndex = 1;
    myswitch .slider("refresh");
    

and for adding a non-form element use .page()

like image 5
Phill Pafford Avatar answered Oct 22 '22 11:10

Phill Pafford