I just started out with RequireJS, but i'm stuck at the part where i want to use one js file which has two defines() in it, like this:
Filename: test.js
define('test1', ['jquery'], function() {
return {
method1 : function () {
console.log("test1 - method 1");
},
method2 : function () {
console.log("test1 - method 2");
}
}
});
define('test2', ['jquery'], function() {
return {
method1 : function () {
console.log("test2 - method 1");
},
method2 : function () {
console.log("test2 - method 2");
}
}
});
I also have a bootstrap,js file which is automatically loaded by the RequireJS framework:
require(['jquery', 'test', 'test2'], function ( $, t1, t2 ) {
console.log(t1);
});
It does find the 2nd param, the 'test' file. Only, it returns a 'null'. It can't find 'test2' because it tries to look for a file called 'test2.js'. Actually i'd like to do something like:
require(['jquery', 'test.test1', 'test.test2'], function ( $, t1, t2 ) {
console.log(t1);
});
But in anyway, i'd like to get a handler to both the objects. What am i doing wrong??
I just started out with RequireJS, but i'm stuck at the part where i want to use one js file which has two defines() in it, like this:
Filename: test.js
define('test1', ['jquery'], function() {
return {
method1 : function () {
console.log("test1 - method 1");
},
method2 : function () {
console.log("test1 - method 2");
}
}
});
define('test2', ['jquery'], function() {
return {
method1 : function () {
console.log("test2 - method 1");
},
method2 : function () {
console.log("test2 - method 2");
}
}
});
I also have a bootstrap,js file which is automatically loaded by the RequireJS framework:
require(['jquery', 'test', 'test2'], function ( $, t1, t2 ) {
console.log(t1);
});
It does find the 2nd param, the 'test' file. Only, it returns a 'null'. It can't find 'test2' because it tries to look for a file called 'test2.js'. Actually i'd like to do something like:
require(['jquery', 'test.test1', 'test.test2'], function ( $, t1, t2 ) {
console.log(t1);
});
But in anyway, i'd like to get a handler to both the objects. What am i doing wrong??
Share Improve this question asked Mar 21, 2012 at 14:46 w00w00 26.8k35 gold badges105 silver badges147 bronze badges 5- The granularity is at the file level. You can't import parts of a file. Just import "test.js" and you'll get both objects. – Pointy Commented Mar 21, 2012 at 14:52
- @Pointy Then there's still something wrong with my code because 'test' returns a null object. – w00 Commented Mar 21, 2012 at 14:57
- Ah ... well according to the RequireJS documentation, there should only be one module in any given .js file. – Pointy Commented Mar 21, 2012 at 15:13
- According to this video its possible. youtube./watch?v=VGlDR1QiV3A --- I did the exact same thing. My first define nu only has a function(){} the second one does have a name defined. But still it's looking for a file called test2.js. --- But it indeed works when i only use one define per file. Guess i'll just stick to that. – w00 Commented Mar 21, 2012 at 15:35
- if you read the ments on that video you can see that other people were confused too. I don't think it's a good explanation at all. – Pointy Commented Mar 21, 2012 at 16:02
1 Answer
Reset to default 16You cannot export two different modules like this. If you want to keep them as "submodules" of 'test', the right way would be to do:
define('test', ['jquery'], function() {
var exports = {};
exports.test1 = {
method1 : function () {
console.log("test1 - method 1");
},
method2 : function () {
console.log("test1 - method 2");
}
};
exports.test2 = {
method1 : function () {
console.log("test2 - method 1");
},
method2 : function () {
console.log("test2 - method 2");
}
};
return exports;
});
Then you can do:
require(['test'], function (test) {
var test1 = test.test1;
});