backbone.js 및 jQuery 템플릿을 사용하고 있습니다. 내가 뭘하고 싶은데요보기의 템플릿을 dom 요소로 설정합니다. 뷰가 초기화 될 때 템플릿이 $ .isFunction로 존재하는 경우 Ajax를 통한 백본보기에서 jQuery 템플릿을 캐시하는 방법은 무엇입니까?
<script id="templateElement" type="text/x-jQuery-tmpl">
<div class='foo'>bar</div>
</script>
, 그것은 볼 수 있습니다 : 그것은 다음과 같이 보입니다. 그렇지 않은 경우 외부 파일에서 가져 와서 반환 된 dom 요소를 body 요소에 추가 한 다음 this.template을 해당 dom 요소로 설정합니다.
다음에보기가 호출되면 해당 dom 요소가 있어야 AJAX 호출을 다시 발행 할 이유가 없어야합니다. 그러나이 템플릿은 AJAX 호출 후에 null이 아니더라도 콜백의 일부로 설정되어 있어도 정의되지 않습니다. #templateElement가 페이지의 일부 임에도 불구하고 뷰가 렌더링 될 때마다 내 AJAX 호출이 실행됩니다.
무슨 일 이니?
BbView = Backbone.View.extend({
template: $('#templateElement').template(),
initialize:
//this.template is null at this point
if(!($.isFunction(this.template))){
$.get('template/file.html', function(templates){
$('body').append(templates);
this.template = $('#templateElement').template();
});
}
//this.template is undefined at this point
...
'this'가 사용되지 않기 때문에 바인드가 필요하지 않지만 적어도 어떻게 작동하는지 볼 수 있습니다. – maxl0rd
감사합니다. 나는 밑줄의 메소드를 모르고 있지만 View.prototype.template을 사용하여 템플릿을 캐시하는 방법을 본다. 이 수정 프로그램을 구현하지는 않았지만 시간이되면됩니다. – tim