Skip to content

Instantly share code, notes, and snippets.

@jmblog
Created June 1, 2012 04:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jmblog/2848784 to your computer and use it in GitHub Desktop.
Save jmblog/2848784 to your computer and use it in GitHub Desktop.
Examples of shim config in RequireJS 2.0 - Backbone and underscore
require.config({
paths: {
underscore: '../underscore-min'
},
shim: {
underscore: {
exports: function() {
return _.noConflict();
}
}
}
});
// Testing with QUnit
require(['underscore'], function(_) {
test('underscore (AMD incompatible library) is loaded correctly', function() {
var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
expect(3);
notEqual(_, undefined);
strictEqual(_, _.noConflict());
deepEqual(evens, [2,4,6]);
});
});
require.config({
paths: {
jquery: '../jquery-1.7.2.min',
underscore: '../underscore-min',
backbone: '../backbone-min'
},
shim: {
/* this configuration is unnecessary
underscore: {
exports: function() {
return _.noConflict();
}
},
*/
backbone: {
deps: ['underscore', 'jquery'],
exports: function() {
return Backbone.noConflict();
}
}
}
});
// Testing with QUnit
require(['backbone'], function(Backbone) {
var Shop = Backbone.Model.extend({});
var ShopList = Backbone.Collection.extend({
model: Shop
});
var ShopView = Backbone.View.extend({
tagName: 'li'
});
module('Backbone Test', {
setup: function() {
this.list = new ShopList([
new Shop({name: 'ikea'}),
new Shop({name: 'muji'}),
new Shop({name: 'uniqlo'})
]);
this.view = new ShopView();
},
teardown: function() {
window.errors = null;
}
});
test('Backbone is loaded correctly', function() {
expect(2);
notEqual(Backbone, undefined);
strictEqual(Backbone, Backbone.noConflict());
});
test('Check dependency on underscore', function() {
expect(1);
var muji = this.list.filter(function(shop) { return shop.get('name').match(/j/); })
equal(muji[0].get('name'), 'muji');
});
test('Check dependency on jQuery', function() {
expect(1);
notEqual(this.view.$el, undefined);
});
});
require.config({
paths: {
jquery: '../jquery-1.7.2.min',
underscore: '../underscore-min',
backbone: '../backbone-min'
},
shim: {
/* this configure is unnecessary
underscore: {
exports: function() {
return _.noConflict();
}
},
*/
backbone: {
deps: ['underscore', 'jquery'],
exports: function() {
return Backbone.noConflict();
}
}
}
});
// Testing with QUnit
require(['underscore'], function(_) {
test('underscore is loaded correctly also in this case', function() {
var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
expect(3);
notEqual(_, undefined);
strictEqual(_, _.noConflict());
deepEqual(evens, [2,4,6]);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment