2017-02-13 5 views
0

2 템플릿 : 하나는 양식 용이고 다른 하나는 결과입니다. 이것이 나의 견해입니다. 양식이 표시되고 있지만 이있는 경우이 행의 정적 템플릿 만 가져오고 <% = key %>이 작동하지 않으며 양식이 사라진다는 의미입니다.jQuery.load를 사용하여 다른 템플릿에 템플릿 표시

APP.FromFront = Backbone.View.extend({ 
    el: $("#content"), 
    events: { 
     "click #addLink" : "addLink", 
     "click .deleteLink" : "deleteLink" 
    }, 
    template: _.template($('#content').html()), 

    initialize: function() { 
     this.bookmarks = new APP.BookmarksCollection(); 
     this.render(); 
    }, 
    render: function() { 
     this.$el.html(this.$el.load("../../templates/form_template.html")); 
     this.bookmarks.each(function(bookmark) { 
      var bookJSON = bookmark.toJSON(); 
      **//############ this is where it's doesn't work** 
      var temp=_.template(this.$el.load("../../templates/links_template.html")); 
      $("#links").append(temp(bookJSON)); 
     },this); 
    }, 
}) 
+0

왜 사용되지 않은 'template : _.template ($ ('content ') .html())가 있습니까? 왜 외부 파일을 사용하지 않고로드하려고합니까? ['load'] (http://api.jquery.com/load/)가 어떻게 작동하는지 이해합니까? 'this. $ el.html (this. $ el.load (url)) '과 같은 코드로 판단 할 때 나는 그렇게 생각하지 않는다. 우리 코드가 동일한 템플릿을 반복해서 가져 오려고한다는 것을 알고 있습니까? 브라우저가이를 캐시 할 수도 있지만 좋은 방법은 아닙니다. ['load'] (http://api.jquery.com/load/)는 비동기식입니다. 먼저 설명서를 읽으십시오. –

+0

답변 해 주셔서 감사합니다. 내 템플릿이 index.html 페이지에있을 때 템플릿 "템플릿"에 사용되었습니다. 이 코드에서는 사용되지 않습니다. 정말로드하고 싶지는 않지만 사용하고 싶습니다. 그것이 index.html에 있지만 다른 파일에는없는 경우 어떻게하는지 알 것입니다. – BoltMaud

답변

2

load은 비동기입니다. 그렇게 처리해야합니다. 또한 동일한 템플리트를 반복적으로 반입하는 것보다로드 된 템플리트를 캐시해야합니다. 다음과 같이 시도하십시오 :

APP.FromFront = Backbone.View.extend({ 
    el: $("#content"), 
    events: { 
    "click #addLink": "addLink", 
    "click .deleteLink": "deleteLink" 
    }, 
    initialize: function() { 
    this.bookmarks = new APP.BookmarksCollection(); 
    this.formTemplatePromise = $.get("../../templates/form_template.html"); 
    this.linkTemplatePromise = $.get("../../templates/links_template.html"); 
    $.when(this.formTemplatePromise, this.linkTemplatePromise) 
    .then(function(formTemplate, linkTemplate) { 
     this.formTemplate = _.template(formTemplate); 
     this.linkTemplate = _.template(linkTemplate); 
     this.render(); 
    }.bind(this)); 
    }, 
    render: function() { 
    this.$el.html(this.formTemplate(/* form data here? */)); 
    var links = []; 
    this.bookmarks.each(function(bookmark) { 
     // below code can be made a 1 liner, I split it for readability 
     var bookJSON = bookmark.toJSON(); 
     var link = this.linkTemplate(bookJSON); 
     links.push(link); 
    }, this); 
    // Single append instead of many for performance 
    $("#links").append(links); 
    } 
}); 
+0

템플릿 용 데이터가 없습니다. 나는 당신의 제안을 시도했으나 효과가 없습니다. 정말 "n은 underscore-min.js에서 정의되지 않았습니다."라는 코드와 함께 "n.replace는 함수가 아닙니다" – BoltMaud

+0

이 옵션을 사용하면 양식이 표시되지 않습니다. – BoltMaud

+0

@ BoltMaud 어떤 버전의 밑줄을 사용하고 있습니까? 나는 당신이 최신 버전을 사용하고 있다고 추정했다. API가 변경되었습니다.이 답변을 참조하십시오. http://stackoverflow.com/a/25881231/2333214 –