jquery mobile骨干:在初始化之前不能在listview上调用方法

前端之家收集整理的这篇文章主要介绍了jquery mobile骨干:在初始化之前不能在listview上调用方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我只是试图结合骨干js和 jquery手机的优势.我正在为移动设备开发,目前正在尝试开发动态列表,用于调试日志消息.想象你有一个控制台窗口,想把条目放在里面.事情是,总是插入一个新的>,列表必须刷新ala’$(‘#myList’).listview(‘refresh’).这不适合我,我得到错误

错误:在初始化之前不能在listview上调用方法;尝试调用方法’刷新’

tagName:’ul’,
id:’console’,

  1. consoleTemplate : _.template($('#console-template').html()),initialize : function() {
  2. console.log('ConsoleView:init');
  3.  
  4. this.$el.attr('data-inset','true');
  5. this.$el.attr('data-role','listview');
  6. this.$el.css('width','50%');
  7. this.$el.append(this.consoleTemplate());
  8.  
  9. // für alle Funktionen die mit this arbeiten
  10. _.bindAll(this,'render','addConsoleItem','appendConsoleItem');
  11.  
  12. this.consoleItemCollection = new ConsoleItemCollection();
  13. this.consoleItemCollection.bind('add',this.appendConsoleItem);
  14.  
  15. this.counter = 0;
  16. this.render();
  17.  
  18. },render : function() {
  19. console.log('ConsoleView:render');
  20.  
  21. var self = this;
  22.  
  23. _(this.consoleItemCollection.models).each(function(item) {
  24. self.addConsoleItem(item);
  25. },this);
  26.  
  27. return this;
  28. },

^^这是我的控制台视图的提取.

  1. var view = Backbone.View.extend({
  2.  
  3. el : 'div',id : 'content',consoleView : null,initialize : function() {
  4. console.log('ApplicationView:init');
  5.  
  6. _.bindAll(this,'render');
  7.  
  8. this.$el.attr('data-role','content');
  9.  
  10. _.bindAll(this,'render');
  11. this.consoleView = new ConsoleView();
  12. this.consoleView.addConsoleItem(new ConsoleItemModel());
  13.  
  14. },render : function() {
  15. console.log('ApplicationView:render');
  16.  
  17. this.$el.append(this.consoleView.render().el);
  18.  
  19. return this;
  20. }
  21.  
  22. });

这是我的应用程序视图.

那么调用刷新方法

谢谢!

解决方法

jQuery Mobile listview需要在刷新之前进行初始化才能被触发:
  1. $('#myList').listview().listview('refresh');

如果您想了解更多信息,为什么在使用jQuery Mobile中动态创建的内容时要小心,请查看我的博客ARTICLE.或者您可以找到它HERE.

猜你在找的jQuery相关文章