Reputation: 45682
Problem:
require.js loads my modules as undefine
s.
Brief description:
If you'd look at main.js
file, you will see that it has TodoItem
dependency. And it's undefine
. The point also is that the body of TodoItem
module is never called.
Question:
Why require.js doesn't execute TodoItem
body and loads undefine
?
My structure:
index.html
js/
|-main.js
|-app/
|-TodoItem.js
|-libs/
|-backbone
|-requirejs
|-underscore
main.js
require([
'app/TodoItem'
], function (TodoItem) {
//PROBLEM: TodoItem is 'undefined'
});
TodoItem.js
define('TodoItem', [], function() {
//Body is never called!
var TodoItem = Backbone.Model.extend({
validate: function(attrs, options) {
if (!attrs.todoMessage)
return 'TodoItem without todo message';
}
});
return TodoItem;
});
Upvotes: 0
Views: 264
Reputation: 6396
app/TodoItem.js
define([], function() {
// backbone model etc..
});
Don't name your module, requireJS will pick it up using the folder structure. In this case requireJS will pick it up from app/TodoItem.js.
If you use optimizer, it will automatically name it for you.
Upvotes: 1