私は cfajaxproxy を初めて使用します。いくつかのチェック ボックスを選択してから、チェックされたすべてのチェックボックスをループし、結果を cfajaxproxy と jQuery を使用してデータベースに保存しようとしています。
マークアップはクエリをループすることによって生成されますが、問題が発生している領域のサンプルを次に示します。
<span id="1569_2627_text">I certify that the employee has been trained in the use of the following
equipment (please check all that apply):</span><br />
<input type="hidden" name="2627_max_length" id="2627_max_length" value="">
<input type="hidden" name="2627_min_value" id="2627_min_value" value="">
<input type="hidden" name="2627_max_value" id="2627_max_value" value="">
<input type="hidden" name="2627_regex_format" id="2627_regex_format" value="">
<input type="hidden" name="2627_system_type" id="2627_system_type" value="">
<input type="hidden" name="2627_app_type_version" id="2627_app_type_version" value="1569">
<input type="hidden" name="2627_question_type" id="2627_question_type" value="CM">
<label>
<input class="questionChoice" type="checkbox" name="2627"
value="8509" data-app_type_version="1569">
<span>Face Shield<span>
</label><br />
<label>
<input class="questionChoice" type="checkbox" name="2627"
value="8510" data-app_type_version="1569">
<span>Neoprene Gloves<span>
</label><br />
<label>
<input class="questionChoice" type="checkbox" name="2627"
value="8511" data-app_type_version="1569">
<span>Apron<span>
</label><br />
<label>
<input class="questionChoice" type="checkbox" name="2627"
value="8512" data-app_type_version="1569">
<span>Boots<span>
</label><br />
<label>
<input class="questionChoice" type="checkbox" name="2627"
value="8513" data-app_type_version="1569">
<span>Wizard Glove<span>
</label><br />
<label>
<input class="questionChoice" type="checkbox" name="2627"
value="8514" data-app_type_version="1569">
<span>Insulated Mitt<span>
</label><br />
<label>
<input class="questionChoice" type="checkbox" name="2627"
value="8515" data-app_type_version="1569">
<span>Insulated Glove<span>
</label><br />
<button class="add_answer" value="2627" data-app_type_version="1569" disabled>Add answer</button>
これが私のcfajaxプロキシタグです:
<cfajaxproxy cfc="#APPLICATION.cfMapping#.Agency.Agency"
jsclassname="agency_object">
チェックボックスごとに実行する関数は次のとおりです。
function saveResponses(question_no, answerValue){
var myagOBJ = new agency_object();
myagOBJ.setHTTPMethod('POST');
myagOBJ.setCallbackHandler(function(result) {
numOfCalls++;
alert(result+ ", call number: "+ numOfCalls);
});
myagOBJ.setErrorHandler(null);
myagOBJ.store_prepopulated_response(
agency_id = #SESSION.agency_object.get_id()#,
jQuery("select##site").val(),
question_no,
answerValue
);
}
各チェックボックスをループする jQuery コードは次のとおりです。
$("div##" + div + " [name=" + question_no + "]:checked").each(function() {
answerText = $(this).next().text();
answerValue = $(this).val();
identifier = question_no + "_" + answerValue;
if(answers["q_" + identifier] === undefined) {
formAppend();
answers["q_" + identifier] = answerValue;
alert("From Checkbox");
saveResponses(question_no, answerValue);
$("div##saved_answers table").append(
"<tr id=\"" + identifier + "\"><td><strong>" + formName +
"</strong><br>" + questionText + "</td><td>" + answerText +
"<br><button data-app_type_version=\"" + div +
"\"class=\"remove\" value=\"" + identifier +
"\">Remove</button></td></tr>"
);
}
});
呼び出している cfc のメソッドは次のとおりです。
<cffunction name="store_prepopulated_response" access="remote" returntype="string" verifyclient="true">
<cfargument name="agency_id" type="numeric" required="true">
<cfargument name="site_id" type="numeric" required="true">
<cfargument name="question_no" type="numeric" required="true">
<cfargument name="response" type="string" required="true">
<cfreturn "Agency id: #agency_id#, Site ID: #site_id#, Question No: #question_no#, Resonse: #response#">
</cffunction>
私はまだそれが機能することを確認するために物事をテストしているだけなので、多くの関数はテスト結果を返す以外は実際に何かをしています.
このコードを実行すると、cfc が正常に呼び出され、結果が返されますが、何度も呼び出されています。
たとえば、3 つのボックスをオンにすると、cfc メソッドが 7 回呼び出され、2 つのボックスをオンにすると、cfc メソッドが 5 回呼び出されます。cfc メソッドが 19 回呼び出される 7 つのボックスすべてをチェックします。
私の最初の考えは、cfc が正しい回数呼び出されている可能性がありますが、結果が返されたときに各インスタンスが応答ハンドラーを呼び出すため、callbackhandler が多く呼び出されているため、agency_object のグローバル インスタンスを作成し、毎回メソッドを呼び出しますが、同じ結果が得られました。
なぜこれが起こるのか誰か知っていますか?
*(編集) 実際の cfc に呼び出し回数も追加しましたが、実際には cfc メソッドの呼び出し回数が多すぎます。