admin管理员组文章数量:1391975
I want to create constructors according to the AMD specification. I found this answer and tried to follow it. Here's what I ended up with:
main.js
requirejs.config({
paths: {
'jquery': 'vendor/jquery-1.9.1.min',
'lodash': 'vendor/lodash-1.3.1.min',
'knockout': 'vendor/knockout-2.2.1.min',
'bootstrap': 'vendor/bootstrap-2.3.2.min'
}
});
requirejs(
['jquery', 'lodash', 'knockout', 'controller/categories'],
function main($,_,ko, CategoriesCtrl) {
var categories = new CategoriesCtrl();
}
);
controller/categories.js
define('categories', function() {
return function CategoriesCtrl(layers) {
var self = this;
layers = layers || [];
console.log(ko);
};
});
The result I get is that CategoriesCtrl is undefined. What have I done wrong?
I want to create constructors according to the AMD specification. I found this answer and tried to follow it. Here's what I ended up with:
main.js
requirejs.config({
paths: {
'jquery': 'vendor/jquery-1.9.1.min',
'lodash': 'vendor/lodash-1.3.1.min',
'knockout': 'vendor/knockout-2.2.1.min',
'bootstrap': 'vendor/bootstrap-2.3.2.min'
}
});
requirejs(
['jquery', 'lodash', 'knockout', 'controller/categories'],
function main($,_,ko, CategoriesCtrl) {
var categories = new CategoriesCtrl();
}
);
controller/categories.js
define('categories', function() {
return function CategoriesCtrl(layers) {
var self = this;
layers = layers || [];
console.log(ko);
};
});
The result I get is that CategoriesCtrl is undefined. What have I done wrong?
Share Improve this question edited May 23, 2017 at 12:08 CommunityBot 11 silver badge asked Jun 28, 2013 at 15:37 Matthew James DavisMatthew James Davis 12.3k7 gold badges63 silver badges91 bronze badges1 Answer
Reset to default 6You have created a named AMD module by making your first argument to define
'categories'
. It's best to avoid this where possible:
You can explicitly name modules yourself, but it makes the modules less portable -- if you move the file to another directory you will need to change the name. It is normally best to avoid coding in a name for the module and just let the optimization tool burn in the module names.
Try adjusting categories.js
to this:
define(function() {
return function CategoriesCtrl(layers) {
// etc
};
});
本文标签: javascriptRequireJS How to define a constructorStack Overflow
版权声明:本文标题:javascript - RequireJS: How to define a constructor? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744689761a2619920.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论