Reputation: 2040
require.config({
paths: {
jquery: 'libs/jquery',
underscore: 'libs/underscore',
}
});
define([
'jquery',
'underscore',
], function($,_){
var test = _.template("hello: <%= name %>",{name:"warren"});
});
Given the above code using requirejs to load jquery and underscore, why am I getting this error...
Uncaught TypeError: Cannot call method 'template' of null
All JS seems to be loading... The error points to the _.template line. This is driving me crazy...
Upvotes: 2
Views: 2673
Reputation: 1918
Are you sure you've bundled the underscore library to be usable with requireJS ?
When you write :
define([
'jquery',
'underscore',
], function($,_){
var test = _.template("hello: <%= name %>",{name:"warren"});
});
The '_' variable (second argument of the callback function) is assigned with the export of the underscore library (second dependency specified).
I guess you used the original version of underscore, which is not package for requireJS. In your underscore.js file, add the following line at the begining:
define(function () {
and this line at the end:
return _;});
Thus, the export are well configured.
Another possible solution is to use the global '_' variable: all you need to do is to remove the second argument of your callback.
define([
'jquery',
'underscore',
], function(){
var test = _.template("hello: <%= name %>",{name:"warren"});
});
From there, your browser will use the '_' global variable, which is defined in underscore, and which is set when the callback is called.
Same behaviour is expected with jQuery.
Upvotes: 7