我在js文件中保存了一个变量:Subcategories.js。像这样的东西(但实际上要大得多):
define({
subcategories: {
"Category1":
[
"Subcategory1-1", "Subcategory1-2", "Subcategory1-3"
],
"Electrical":
[
"Subcategory2-1", "Subcategory2-2", "Subcategory2-3", "Subcategory2-4"
],
},
getSubcategories: function () {
return this.subcategories;
}
});
我已经成功地通过这种方式将变量成功发送到SomethingController.js(仅复制了下面必要代码的一部分):
define(
[
"underscore",
"base/Controller",
"views/SomethingView",
"collections/SomethingCollection",
"helpers/Subcategories",
],
function(_, Controller, SomethingView, SomethingCollection, Subcategories) {
var subcategory = null,
var SomethingController = Controller.extend({
show_results: function(collection, resp) {
var data = {
subcategories: Subcategories.getSubcategories(),
};
...
但是当我尝试在SomethingView.js中导入变量时,仅当我再次使用整个json对象时,它才有效:
define(
[
"jquery",
"backbone",
"has/form",
"has!input-attr-placeholder?:jqueryui/placeholder",
"scripts/libs/jquery.tmpl.js",
"helpers/Subcategories",
],
function( $, Backbone, viewTemplate, has, Subcategories) {
var SomethingItemsView = Backbone.View.extend({
el: $("#main-section"),
template: viewTemplate,
events: {
"click #some_button" : "change_some_page",
},
something_items: function( event ) {
event.preventDefault();
var somethingCategory = $("#some_selector");
var subcategories = {// entire json object
}
var somethingSubcategory = subcategories[somethingCategory];
...
我已经尝试过var subcategories = Subcategories.getSubcategories()
,Subcategories.subcategories
和各种各样的东西。浪费了相当多的时间来做这么小的事情。我为为什么它不起作用而感到困惑,并且我不想在那里对整个对象进行硬编码。
我正在对现有网站的前端进行一些更改,因此请注意,除了Subcategories.js之外,我无法更改任何这些文件的框架。
任何意见将不胜感激。
答案 0 :(得分:0)
我认为您的问题出在您依赖项的顺序上:
[
"jquery",
"backbone",
"has/form",
"has!input-attr-placeholder?:jqueryui/placeholder",
"scripts/libs/jquery.tmpl.js",
"helpers/Subcategories",
],
function( $, Backbone, viewTemplate, has, Subcategories) {
必须具有相同的顺序。子类别是第六个也是最后一个参数,但是您只有五个输入。
jquery => $
backbone => Backbone
has/form => viewTemplate
has!... => has
scripts/libs/jquery.tmpl.js => Subcategories
helpers/Subcategories => OUPS NOTHING O_O
希望有帮助