【发布时间】:2010-09-20 02:10:59
【问题描述】:
我最近一直在努力理解组织 jQuery 代码的最佳方式。我之前问了另一个问题,我认为我不够具体 (found in this question here)。
我的问题是,您制作的应用程序越丰富,您的客户端就会越快失控。考虑一下这种情况...
//Let's start some jQuery
$(function() {
var container = $("#inputContainer");
//Okay let's list text fields that can be updated
for(var i=0; i < 5; i++) {
//okay let's add an event for when a field changes
$("<input/>").change(function() {
//okay something changed, let's update the server
$.ajax({
success:function(data) {
//Okay - no problem from the server... let's update
//the bindings on our input fields
$.each(container.children(), function(j,w) {
//YIKES!! We're deep in here now!!
$(w).unbind().change(function() {
//Then insanity starts...
}); // end some function
}); //end some loop
} // what was this again?
}); //ending something... not sure anymore
}).appendTo(container); //input added to the page... logic WAY split apart
}; //the first loop - whew! almost out!
}); //The start of the code!!
现在这种情况并非不可能。我并不是说这是正确的做法,但是发现自己进入 jQuery 命令的几个级别并开始想知道在屏幕开始融化之前还能添加多少逻辑。
我的问题是人们如何管理或组织以限制其代码的复杂性?
【问题讨论】:
-
.changed(... 应该是 .change(...
-
你说得对——我是凭记忆做的……这对我来说不是最好的……
-
您能编辑帖子并更正它吗?在我阅读其中一个答案时让我感到困惑。
-
@Hugoware 我认为你应该接受这个答案:)
标签: javascript jquery code-organization