如何在zepto中使用requirejs

Ric*_*ann 6 javascript frontend requirejs backbone.js zepto

我似乎无法使用requirejs来使用zepto.

这是我的文件

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();
});
Run Code Online (Sandbox Code Playgroud)

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
  };
});
Run Code Online (Sandbox Code Playgroud)

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
  };
});
Run Code Online (Sandbox Code Playgroud)

你得到了图片..但是当我运行这一切时,我在Chromes控制台中得到了这个:

GET http://localhost/SBApp/www/js/jquery.js 404 (Not Found)         require.js:1824
Run Code Online (Sandbox Code Playgroud)

和一个脚本错误(我扔在括号bc这不会让我发布.)

在带有firebug的Firefox中,它会发出一个脚本错误

有没有人成功配置zepto with require并可以给我一些帮助?

gwi*_*g33 1

Backbone 通常有一个“define(["underscore","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; } );
}
Run Code Online (Sandbox Code Playgroud)

似乎有效。如果你想将 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...
});
Run Code Online (Sandbox Code Playgroud)

但是通过这样做,我不必修改 jquery 的backbone.js 定义文件,并且我引入了对 IE 的浏览器支持...