我看起来不能让zepto和requirejs一起工作。
这是我的文件
main.js
require.config({
paths: {
zepto: 'libs/zepto/zepto.min',
underscore: 'libs/underscore/underscore-min',
backbone: 'libs/backbone/backbone-min',
cordova: 'libs/cordova/cordova-2.1.0',
history: 'libs/history/history',
historyZ: 'libs/history/history.adapter.zepto'
},
shim: {
zepto: {
exports: '$'
},
backbone: {
deps: ['underscore', 'zepto']
}}
});
require([
// Load our app module and pass it to our definition function
'app',
], function(App){
// The "app" dependency is passed in as "App"
App.initialize();
});app.js
define([
'zepto',
'underscore',
'backbone',
'router' // Request router.js
], function($, _, Backbone, Router){
var initialize = function(){
// Pass in our Router module and call it's initialize function
Router.initialize();
}
return {
initialize: initialize
};
});router.js
define([
'zepto',
'underscore',
'backbone',
'views/dashboard'
], function($, _, Backbone, DashboardView){
var AppRouter = Backbone.Router.extend({
routes: {
// Define some URL routes
'' : 'showDashboard',
}
});
var initialize = function(){
var app_router = new AppRouter;
app_router.on('showDashboard', function(){
// We have no matching route, lets just log what the URL was
//console.log('No route:', actions);
var dashboardView = new DashboardView();
dashboardView.render();
});
Backbone.history.start();
};
return {
initialize: initialize
};
});你就明白了..但是当我运行这一切时,我在Chromes控制台中得到了如下结果:
GET http://localhost/SBApp/www/js/jquery.js 404 (Not Found) require.js:1824还有一个脚本错误(我抛出了bc括号,这不会让我发布的。)
在Firefox的firebug中,它吐出了一个脚本恐怖
有没有人成功地用require配置了zepto,并能给我一些帮助?
发布于 2012-12-19 04:34:21
Backbone通常有一个“define(”下划线“,”"jquery“”,"exports",..“在它里面,应该只需要替换掉它。然后,我在zepto.js的末尾附加了以下代码片段。
// If `$` is not yet defined, point it to `Zepto`
window.Zepto = Zepto
'$' in window || (window.$ = Zepto)
if ( typeof define === "function" && define.amd ) {
define( "zepto", [], function () { return Zepto; } );
}它似乎起作用了。如果你想用jquery作为备份(这很脏),但是我在zepto.js文件中把"zepto“定义为"jquery”,那么在requirejs.config中我就这样做了…
requirejs.config({
paths: {
// Jquery should be zepto, probably a better way to do this...
jquery: RegExp(" AppleWebKit/").test(navigator.userAgent) ? '/js/vendor/zepto' : '/js/vendor/jquery.min',
underscore: '/js/vendor/underscore.min',
backbone: '/js/vendor/backbone.min'
}
});
require(['jquery', 'underscore', 'backbone'], function($, _, Backbone) {
// Stuff here...
});但是通过这样做,我不必修改jquery的backbone.js定义文件,并且我引入了对IE的浏览器支持……
发布于 2013-06-25 02:02:24
使用requirejs的shim特性。请参阅此answer。避免每次发生这种情况时都必须编辑库的源代码。此外,您不必记住在每次将库更新到较新版本时都进行编辑。
添加这个免责声明,因为@James Watkins认为每个帖子都必须对他有效,否则它应该被否决:这个解决方案可能对你有效,也可能不适用(特别是考虑到它是在3年前写的!__)
发布于 2014-07-12 10:15:11
你可以添加一个'extend/zepto.js‘文件,而不是修改Zepto.js:
/**
* extend Zepto
*/
define([
'zepto'
], function() {
"use strict";
window.Zepto = Zepto
// If `$` or `jQuery` is not yet defined, point them to `Zepto`
'$' in window || (window.$ = Zepto)
'jQuery' in window || (window.jQuery = Zepto)
return Zepto;
});然后将jquery路径设置为extend/zepto
require.config({
paths: {
'jquery': 'extend/zepto'
}
})https://stackoverflow.com/questions/13425815
复制相似问题