所以,我在jsfiddle中有一个例子,我需要一些帮助。我有一个html中的表单,以及我想用它填充的数据。我已经在命名空间定义中包含逻辑来执行此操作,我认为这是正确的,但JsHint认为不是,是的,我的数据没有在表单控件中呈现。我很感激帮助解决这个问题。绝对不会使用Jquery,因为我只使用Ext.js lib。提前致谢。
[JSFiddle链接到我的例子] [1]
var data = {
Tasks = [{
"taskid": 1,
"author": "Bill Maher",
"description": "Purple Rain purple",
"dateCreated": "12/23/2013",
"dataModified": "2/23/2013",
"dueDate": "2/30/2014",
"status": "in progress"
}, {
"taskid": 2,
"author": "Seth Green",
"description": "I am not certain this is needed",
"dateCreated": "3/24/2011",
"dataModified": "",
"dueDate": "5/3/2011",
"status": "completed"
}, {
"taskid": 3,
"author": "Arnold Schwatsheneggar",
"description": "I would of course like to have data to test with",
"dataModified": "",
"dueDate": "",
"status": "in progress"
}, {
"taskid": 4,
"author": "Lilly blue",
"description": "make the sun shine high again",
"dateCreated": "4/12/2014",
"dataModified": "",
"dueDate": "5/10/2014",
"status": "started"
}, {
"taskid": 5,
"author": "Sam Raj",
"description": " when will I see you again",
"dateCreated": "",
"dataModified": "",
"dueDate": "",
"status": "in progress"
}, {
"taskid": 6,
"author": "Kate Kurtmann",
"description": "Show me that you love me. See ya!",
"dateCreated": "",
"dataModified": "",
"dueDate": "",
"status": "in progress"
}, {
"taskid": 7,
"author": "Kristen BenBazza",
"description": "I am a real American",
"dateCreated": "3/4/2013",
"dataModified": "12/3/14",
"dueDate": "5/23/2014",
"status": "in progress"
}, {
"taskid": 8,
"author": "Venkat Shack",
"description": "You are the bravest of hearts, you are the strongest of souls",
"dateCreated": "12/1/2001",
"dataModified": "12/12/2003",
"dueDate": "7/6/2003",
"status": "started"
}, {
"taskid": 9,
"author": "Sunny Chan",
"description": "WHat the f is FADs anyway",
"dateCreated": "12/1/2011",
"dataModified": "3/12/2013",
"dueDate": "10/10/2014",
"status": "completed"
}, {
"taskid": 10,
"author": "William Rolloff",
"description": "Accounting for the costs improving care",
"dateCreated": "2/12/2013",
"dataModified": "12/01/2014",
"dueDate": "10/15/2015",
"status": "completed"
}, {
"taskid": 11,
"author": "Aakash Khandari",
"description": "Making a move to a better life and career",
"dateCreated": "4/3/2000",
"dataModified": "4/7/2005",
"dueDate": "7/17/2014",
"status": "in progress"
}
]
};
// more code goes here but has been deleted for brevity
//revealing public API
return {
exporter.tracy: {//namespace definition
data = data,
trainingTask: {//second namespace
add = addTask,
update = UpdateTask,
load = loadDetail,
clearDetail = clearForm,
save = SubmitTask,
remove = deleteRecord,
expandGroup = groupexpand,
collapseGroup = groupcollapse,
toggleGroup = toggleGroup,
fillMenu = fillMenu,
setGroupStyle = setGroupStyle,
isGrouped = isGrouped
};
};
};
};
/*ending of the module*/
}(this)); //close tracy.trainingtask
答案 0 :(得分:1)
您在闭包内声明并定义TaskSetJson
(自执行匿名函数),这对于模块化您的代码是正确的。
但是你忘了揭露它。
您可以使用RMP(Revealing Module Pattern),如下所示:
tracy = (function() {
var foo = function(a,b,c) { ... };
var var = function(e,f,g) { ... };
var private_value = 1;
var public_value = 2;
// "Reveal" the public parts of your module
return {
foo: foo,
var: var,
public_value: public_value
};
})();
使用此模式,您的变量和函数将在闭包(匿名函数)中声明。因此,它们只能在封闭内使用。但是你可以通过返回它们来使它们在闭包之外可用。
如果要扩展现有的全局变量,而不是返回模块的显示部分,可以将其传递给自执行函数,并直接附加到它:< / p>
(function(tracy) {
var foo = function(a,b,c) { ... };
var var = function(e,f,g) { ... };
var private_value = 1;
var public_value = 2;
// "Reveal" the public parts of your module
tracy = {
foo: foo,
var: var,
public_value: public_value
};
})();
这两个示例略有不同,但在这两种情况下,您都可以致电tracy.foo
,tracy.var
或访问tracy.public_value
。
第一个示例更加灵活,因为您可以获得模块的多个独立实例,并将它们存储在不同的变量中。
答案 1 :(得分:0)
var
声明一个变量,可选择将其初始化为一个值。引用mdn:
用var声明的变量的范围是它的当前执行上下文,它是封闭函数,或者对于在任何函数外部声明的变量,是全局的。
在您的jsfiddle中,您似乎希望能够访问函数内定义的变量,但这是不可能的。
我打算把你的小提琴分开,但它很大。以下是使用“命名空间”的一个小例子,请记住,的目的是不污染全局范围:
(function(export) {
var some_private_variable;
var some_other_private_variable;
function loadDetail () {
}
var taskSet = [{
//a task
}];
export.Tracy = {
trainingTask: {
load: loadDetail,
add: addTask
},
TaskSet: taskSet
};
})(this);
由于this
是执行此操作时的全局对象(窗口),因此export
是窗口对象。所以你可以在后面使用它:
Tracy.trainingTask.load(..)