2

我正在使用jquery mobile、phone gap 和backbone.js 制作一个应用程序。在此我动态创建页面并将其附加到 html 页面的正文元素。我还在为特定页面动态创建列表视图。然而,列表视图只显示 li 标签的普通链接。我的视图的代码如下

directory.views.UserListPage = Backbone.View.extend({

 initialize: function() {
    this.template = _.template(directory.utils.templateLoader.get('user-list-page'));
},

 events:{ "click .add-button": "addButton_clicked"
},
render: function(eventName) {
    $(this.el).html(this.template(this.model.toJSON()));
    this.listView = new directory.views.UserListView({el: $('ul', this.el),model: this.model});
    this.listView.render();

    $content = $(this.el).children(":jqmData(role=content)");
  $(this.el).appendTo($("body")).page();

    $content.find(":jqmData(role=listview)" ).listview();
    return this;
},

addButton_clicked: function(event) {
console.log("clicked");
    event.preventDefault();
   directory.app.navigate("addUser", {trigger: true});
}});

directory.views.UserListView = Backbone.View.extend({


initialize: function() {
    this.model.bind("reset", this.render, this);
},

render: function(eventName) {
    $(this.el).empty();
    _.each(this.model.models, function(user) {
        $(this.el).append(new directory.views.UserListItemView({model: user}).render().el);
    }, this);
    return this;
}});

directory.views.UserListItemView = Backbone.View.extend({

tagName: "li",

events:
{
"click" : "borrowHistory"
}
,
initialize: function() {
    this.template = _.template(directory.utils.templateLoader.get('user-list-item'));
},

render: function(eventName) {
    $(this.el).html(this.template(this.model.toJSON()));
    return this;
} ,
borrowHistory: function(event) {



    var children = $(event.currentTarget).children();
    var attribute = children[0].dataset.id;
   var url = "#/rs/" + attribute +  "/borrowHistory";

   directory.app.navigate(url, {trigger: true});
   return false;
}});

我的模板看起来像这样

<div data-role="header" data-theme="e">
    <a href="#addUser" data-role="button" data-icon="plus" >Add</a>
    <h1>Borrow Mate</h1>

  </div>
  <div id="listUser" data-role="content"  >
  <div id="listcontent">
     <ul  id="userList" data-role="listview" data-inset="true" data-theme="e" data-divider-theme="e" data-count-theme="e"> </ul>
    </div>
  </div>

用户列表项

<div class="userListItem" data-id = "<%= id %>" >
    <b><%= Name %></b>
    <span class="ui-li-count"><%= Credits %></span>
</div>

添加了我的输出的屏幕截图

4

4 回答 4

4

您需要刷新列表视图

因此,在您附加需要刷新的列表项之后,就像这样

$('#userList').listview('refresh');

使用您提供的模板的 id 属性

<ul id="userList" data-role="listview" data-inset="true" data-theme="e" data-divider-theme="e" data-count-theme="e">
</ul>
于 2012-06-20T13:20:35.670 回答
0

如果样式不适用于特别 li 元素。检查与 li 关联的类。让它太刷新。

以以下格式签出

$(#userList .userListItem).listview("刷新");

于 2015-03-14T11:48:54.920 回答
0

您需要确保在“导航”到您的页面之前调用了 render()。

这样,jQuery Mobile 就有时间在内容显示之前对其进行标记。

我通过使用 pub/sub 委托渲染和导航事件解决了这个问题,因此它们在正确的时间(数据到达后)被调用,这消除了您面临的竞争条件问题。

于 2013-08-13T10:19:50.943 回答
0

您可能需要在 pageshow 事件中调用 listview 上的 refresh 方法,或者如果这是第一次创建它,则可能需要调用 create 方法。

例如

$(document).delegate('#yourPage', 'pageshow', function (event, ui) {
        $('#userList').listview('refresh');
          //or surround in a try catch block
       /* try {
           $('#userList').listview();
          } catch {
           $('#userList').listview('refresh');
         }*/
    }); 
于 2012-06-20T14:48:15.440 回答