我真的很纠结于require.js和jquery mobile。我有一个基于松散的文件结构和加载模式,基于
https://github.com/appboil/appboil-requirejs-backbonejs-jquerymobile-phonegap
但是它太旧了,我不得不为Require2.0版本做一些调整。有没有一种社区认可的方式来同时使用jquery mobile,backbonejs和requirejs?我想使用backbone的路由,而不是jquery手机。此外,该模板有phonegap,我不关心这一点。
发布于 2012-06-09 02:01:30
下面是我使用的main.js文件...
require.config({
baseUrl: "/js/",
paths: {
jquery: 'libs/jquery/jquery-1.7.1',
'jquery.mobile-config': 'libs/jqm/jquery.mobile-config',
'jquery.mobile': 'libs/jqm/jquery.mobile-1.1.0',
'jquery.mobile.asyncfilter': 'libs/jqm/asyncfilter',
underscore: 'libs/underscore/underscore-1.3.3',
backbone: 'libs/backbone/backbone-0.9.2',
templates: '../templates'
},
shim: {
'underscore': {
exports: "_"
},
'backbone': {
//These script dependencies should be loaded before loading
//backbone.js
deps: ['jquery','underscore'],
//Once loaded, use the global 'Backbone' as the
//module value.
exports: 'Backbone'
},
'jquery.mobile-config': ['jquery'],
'jquery.mobile': ['jquery','jquery.mobile-config'],
'jquery.mobile.asyncfilter': ['jquery.mobile'],
}
});
require([
'jquery',
'app',
'jquery.mobile','jquery.mobile.asyncfilter'
], function( $, App ){
$(function(){
App.initialize();
});
});
最后一位对于让JQM正确加载(以及实际运行)非常重要。这部分:
require([
'jquery',
'app',
'jquery.mobile','jquery.mobile.asyncfilter'
], function( $, App ){
$(function(){
App.initialize();
});
});
因为我需要jquery for jqm (jquery mobile),所以我将加载它们,多亏了上面的shim代码,依赖项才能以正确的顺序加载。我实际上没有向函数调用传递任何jqm变量,它只传递$和App。下一个重要的部分是jqm-config文件:
define(['jquery'], function ($) {
$(document).on("mobileinit", function () {
$.mobile.ajaxEnabled = false;
$.mobile.linkBindingEnabled = false;
$.mobile.hashListeningEnabled = false;
$.mobile.pushStateEnabled = false;
});
});
您可以将jqm的所有preinit代码放在该文件中。完成所有这些之后,您应该能够使用jqm了!
发布于 2012-06-06 14:22:38
你可以查看backbonejs boilerplate的最新版本,或者查看christophe的backbone directory,它包含了启动basic应用程序所需的所有内容。
编辑
https://github.com/raDiesle/backbone.js-jquerymobile-boilerplate-template
发布于 2012-10-27 22:45:05
我刚刚在jQuery移动文档中添加了一个backbone.js、require.js和jQuery移动小示例应用程序。如果有帮助,请让我知道:
http://demos.jquerymobile.com/1.4.4/backbone-requirejs/backbone-require.html
https://stackoverflow.com/questions/10904433
复制相似问题