2

I'm using Handlebarsjs inside a jquery mobile page and when I get the data posted back from the server, it gets inserted into the template ... but none of the jquery mobile CSS styles are applied to the code inside the template.

My template looks like this:

            <script id="card_list_template" type="text/x-handlebars-template">
            {{#each this}}
            <li>
                <a href="#">
                    <img src="{{path}}" />
                    <h3>{{first_name}}&nbsp;{{last_name}}</h3>
                    <p>Card Owner:&nbsp;{{user_id}}</p>
                </a>
            </li>
            {{/each}}
        </script>

The above code block is inserted into this unordered list:

<ul id="search_results" data-role="listview" data-theme="a">

The data-role="listview" is supposed to apply a certain style to the list items, but it's not working for the items generated through the template.

Dustin Kofoed
  • 539
  • 1
  • 8
  • 17
  • possible duplicate of [jQuery Mobile does not apply styles after dynamically adding content](http://stackoverflow.com/questions/7999436/jquery-mobile-does-not-apply-styles-after-dynamically-adding-content) – mu is too short Jul 29 '12 at 09:06
  • nothing?? i'm sure someone's run into this before ... – Dustin Kofoed Aug 01 '12 at 02:25

2 Answers2

9

If you call .trigger('create') on the element it should apply the styles.

Example:

var template = Handlebars.compile(source);
var html = template(output);
$('#handlebarsData').html(html);
// Applies jQuery Mobile styles
$('#handlebarsData').trigger('create');
Baz
  • 36,440
  • 11
  • 68
  • 94
rickdmer
  • 1,147
  • 12
  • 12
0
.trigger('create')

didn't help me with unordered list. But

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

did.

humkins
  • 9,635
  • 11
  • 57
  • 75