Jquery mobile jQueryMobile:UncaughtTypeError:无法调用方法'_触发器';未定义的
我正在使用jQuery Mobile with backbone.js。加载主页时,出现以下错误:Jquery mobile jQueryMobile:UncaughtTypeError:无法调用方法'_触发器';未定义的,jquery-mobile,backbone.js,Jquery Mobile,Backbone.js,我正在使用jQuery Mobile with backbone.js。加载主页时,出现以下错误: Uncaught TypeError: Cannot call method '_trigger' of undefined 这就是我加载主页的方法。 在routes.js中: routes:{ '':'home', } home:function () { new HomeView(); this.changePage(new HomeContentView());
Uncaught TypeError: Cannot call method '_trigger' of undefined
这就是我加载主页的方法。
在routes.js中:
routes:{
'':'home',
}
home:function () {
new HomeView();
this.changePage(new HomeContentView());
},
changePage:function (page) {
$(page.el).attr('data-role', 'page');
console.log($(page.el));
page.render();
$('body').append($(page.el));
var transition = $.mobile.defaultPageTransition;
if (this.firstPage) {
transition = 'none';
this.firstPage = false;
}
$.mobile.changePage($(page.el), {changeHash:false, transition: transition});
}
in view.js
window.HomeView = Backbone.View.extend({
template : Handlebars.compile($('#home').html()),
render : function (eventname) {
this.$el.html(this.template());
this.header = new HeaderElement();
this.$el.find('div.header_element').append(this.header.$el);
this.footer = new FooterElement();
this.$el.find('div.footer_element').append(this.footer.$el);
return this;
}
});
window.HomeContentView = Backbone.View.extend({
initialize: function(options) {
this.collection = new Fan();
this.template = Handlebars.compile(tpl.get('elements/home'));
//~ console.log(tpl.get('home'));
this.collection.on("reset",this.render,this);
this.init = true;
if (this.init) {
upLimit = 1;
this.collection.index();
this.init = false;
}
},
el: '#home_content_view',
render : function (eventName) {
var self = this;
var js = (self.collection.toJSON())[0];
console.log(js);
$('#home_content_view').html(self.template(js));
$('#home_content_view').trigger("create");
}
});
在home.html中
<div data-role="content">
hi
</div>
其他页面被正确呈现。只有主页给我带来了麻烦。
我哪里做错了?如何解决这个问题?我认为这个错误可能是因为jQuery Mobile需要一个元素来在
changePage
上进行转换。这是一个黑客攻击,但是在index.html中将数据角色属性设置为“page”的空div
应该可以解决这个问题:
<body>
<!-- jQM seems to need a page to exist in the document before it transitions to the first dynamically generated one -->
<div data-role="page"></div>
</body>
我发现问题的根源是jquery mobile 1.3.0版。当我回到JSM1.2.0或1.2.1时,“uncaughtTypeError:cannotcallmethod'trigger'of undefined”问题就消失了
顺便说一句,我没有使用主干网,但我遇到了问题。尝试使用Jquery mobile 1.3.0的最新版本,我在没有主干网的情况下也遇到了同样的错误,但动态生成了一个页面。我正在删除一个页面并重新创建,但是changePage失败了。这很好,为什么它不是答案呢??
<body>
<!-- jQM seems to need a page to exist in the document before it transitions to the first dynamically generated one -->
<div data-role="page"></div>
</body>