问题
I recently started a Javascript project and I am now moving it to require.js. Everything worked fine so far except for the spin.js library. I get the following error message when I try to access anything spin.js related:
Uncaught ReferenceError: Spinner is not defined
My requirejs.config looks like this:
requirejs.config({
baseUrl: 'js',
paths: {
'jquery': 'lib/jquery',
'spin': 'lib/spin',
},
shim: {
'jquery' : {
deps: [],
},
'spin' : {
deps: [],
exports: 'Spinner'
},
}
});
A sample module looks like this:
require(['spin'],
function(Spinner)
{
new Spinner();
}
);
I'm using the shim config because I have some other modules with dependencies. Everything else seems to be loading fine though. What am I missing here?
Edit:
As Alex pointed out my library inclusion was wrong. For everyone having troubles with understanding backbone.js and require.js I suggest this book, especially the chapter about modular development.
回答1:
The spin library should not be shimmed in your config. From the spin.js source code:
if (typeof define == 'function' && define.amd)
define(function() { return Spinner })
else
window.Spinner = Spinner
It is already defined as a module here at the end, and window.Spinner is not created as a window object (which is why it shouldn't be shimmed)
回答2:
I had a case of something similar. I didn't add to shim but neglected to add Spinner to the function after Backbone which caused it to be undefined.
define([
'jquery',
'underscore',
'backbone',
'spin'
],
function($, _, Backbone, Spinner) {
var SpinnerView = Backbone.View.extend({
initialize: function() {
this.loadingAnimation();
},
来源:https://stackoverflow.com/questions/15065463/loading-spin-js-with-require-js