我在cakephp,backbone和requirejs中開發了一個站點。RequireJS在其他文件中使用路徑
進入我的配置文件我已經映射了我的javascript,如果我調用jquery到這個文件中,所有的工作都很好。 如果我打電話給另一個文件jquery給我錯誤,該函數是不確定的,就像沒有加載jquery。
這是我FOLE工作正常config.js:
require.config({
baseUrl: "/site.com/js/",
paths: {
// Aliases for libraries, so that we can change versions from here
jquery: "libs/jquery/jquery-2.0.2.min",
handlebars: "libs/backbone/template/handlebars",
lodash: "libs/backbone/template/lodash.min",
backbone: "libs/backbone/backbone-1.0.0.min",
less:"libs/less/less-1.4.0-beta.min",
helper:"app/helper",
jquery_cookie:"libs/jquery/plugin/jquery.cookie",
text:"libs/require/text-2.0.7"
},
shim: {
"lodash": { exports: '_' },
"handlebars": { exports: 'Handlebars' },
"jquery": { exports: '$' },
"backbone": {
//These script dependencies should be loaded before loading
//backbone.js
deps: ["helper", "lodash", "jquery"],
//Once loaded, use the global "Backbone" as the
//module value.
exports: "Backbone"
},
"jquery_cookie": {
deps: ["jquery"]
//,exports:"$"
},
"less": {
exports:"less"
}
}
// This is appended to every module loading request, for cache invalidation purposes
// comment it on production
, urlArgs: "bust=" + (new Date()).getTime()
});
require(["jquery"], function ($) {
$("body").empty();
});
相反,如果我寫這篇文章到config.js
require.config({
baseUrl: "/site.com/js/",
paths: {
// Aliases for libraries, so that we can change versions from here
jquery: "libs/jquery/jquery-2.0.2.min",
handlebars: "libs/backbone/template/handlebars",
lodash: "libs/backbone/template/lodash.min",
backbone: "libs/backbone/backbone-1.0.0.min",
less:"libs/less/less-1.4.0-beta.min",
helper:"app/helper",
jquery_cookie:"libs/jquery/plugin/jquery.cookie",
text:"libs/require/text-2.0.7"
},
shim: {
"lodash": { exports: '_' },
"handlebars": { exports: 'Handlebars' },
"jquery": { exports: '$' },
"backbone": {
//These script dependencies should be loaded before loading
//backbone.js
deps: ["helper", "lodash", "jquery"],
//Once loaded, use the global "Backbone" as the
//module value.
exports: "Backbone"
},
"jquery_cookie": {
deps: ["jquery"]
//,exports:"$"
},
"less": {
exports:"less"
}
}
// This is appended to every module loading request, for cache invalidation purposes
// comment it on production
, urlArgs: "bust=" + (new Date()).getTime()
});
和呼叫轉移到jQuery的進入default.thtml中檢索我未定義函數的錯誤,因爲我不認爲加載jQuery。
default.thtml中
require(["jquery"], function ($) {
$("body").empty();
});
爲什麼我不能使用jquery到deafult.ctp? 我必須映射到所有文件?我不這麼認爲,有人能幫助我嗎? 謝謝
這仍然是一個問題?如果是的話,你是如何克服的? – w3jimmy