ajax 호출 (필요한 JSON을 제공함)이 성공하면 트리거 된 함수에서 underscore.js를 사용하여 템플릿을 렌더링하려고합니다. AJAX 호출이 처음으로 성공하면Underscore.js - 처음에 템플릿이 컴파일되지 않습니다.
- , 나는이 오류가 :
나는 이상한 행동의 어떤 종류를 겪고있어
Uncaught ReferenceError: response is not defined
- 가에 대한 성공 두 번째로 페이지를 새로 고침하지 않고 모든 것이 의도 한대로 진행됩니다.
내 JSON이 구조를 가지고 :
{
data: [
item1: {count: "1", url: "http://example1.com", id:"arstd", formatted:"1"},
item2: {count: "53", url: "http://example2.net", id:"hneio", formatted:"0"},
...
]
}
내 underscore.js 템플릿 :
<script type="text/template" id="count_template">
<% _.each (response.data, function (item) { %>
<a href="<%- item.url %>">
<li>
<p id="<%- item.id %>" class="name">
<%- item.formatted %>
</p>
<p id="<%- item.id %>_count" class="count">
<%- item.count %>
</p>
</li>
</a>
<% }); %>
</script>
내 아약스 콜백 기능 :
var on_result_count_fetched = (function() {
var totals = $(".regions .count");
var ajax_loader = $("#ajax_loader");
var c_tl = $('#count_template');
var count_div = $('#res_count');
//getting the template for the count response
if (c_tl) {
var c_template = _.template(c_tl.html());
}
_.templateSettings.variable = "response";
//real callback
return function (response) {
if (response.redirect) {
window.location.replace(data.redirect);
} else {
//hide loading animation
ajax_loader.hide();
if (!response && _.isEmpty(response)) {
var tmp = $("<button>In case of fail: do this other action!</button>")
tmp.click (function() {
fetch_results ("searx");
});
} else {
console.log(response);
var tmp = c_template(response);
}
count_div.empty();
count_div.append(tmp);
}
}
}());