9

我正在尝试结合 Backbone.js 和 jQuery mobile 的优点。我正在为移动设备开发,目前正在尝试开发一个动态列表,用于调试日志消息。想象一下,你有一个控制台窗口,你想在里面放条目。问题是,在插入一个新<li>的 之后,必须用 刷新列表$('#myList').listview('refresh')。这对我不起作用,我收到此错误:

错误:无法在初始化之前调用列表视图上的方法;试图调用方法“刷新”

tagName : 'ul',
id : 'console',
consoleTemplate : _.template($('#console-template').html()),
initialize : function() {
  console.log('ConsoleView:init');
  this.$el.attr('data-inset', 'true');
  this.$el.attr('data-role', 'listview');
  this.$el.css('width', '50%');
  this.$el.append(this.consoleTemplate());
  // für alle Funktionen die mit this arbeiten
  _.bindAll(this, 'render', 'addConsoleItem', 'appendConsoleItem');
  this.consoleItemCollection = new ConsoleItemCollection();
  this.consoleItemCollection.bind('add', this.appendConsoleItem);
  this.counter = 0;
  this.render();
},
render : function() {
  console.log('ConsoleView:render');
  var self = this;
  _(this.consoleItemCollection.models).each(function(item) {
    self.addConsoleItem(item);
  }, this);
  return this;
},

这是我的控制台视图的摘录。

var view = Backbone.View.extend({
  el : 'div',
  id : 'content',
  consoleView : null,
  initialize : function() {
    console.log('ApplicationView:init');
    _.bindAll(this, 'render');
    this.$el.attr('data-role', 'content');
    _.bindAll(this, 'render');
    this.consoleView = new ConsoleView();
    this.consoleView.addConsoleItem(new ConsoleItemModel());
  },
  render : function() {
    console.log('ApplicationView:render');
    this.$el.append(this.consoleView.render().el);
    return this;
  }
});

这是我的应用程序视图。

那么什么时候调用刷新方法呢?

谢谢!

4

1 回答 1

27

jQuery Mobile在触发刷新之前需要初始化listview:

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

如果你想了解更多关于这个以及为什么在 jQuery Mobile 中使用动态创建的内容时要小心很重要,请查看我的博客文章。或者你可以在这里找到它。

于 2013-01-29T14:58:50.113 回答