我得到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);
},
})
爲什麼你有一個未使用的'template:_.template($('#content')。html()),'?你爲什麼要加載外部文件而不是使用它?你知道['load'](http://api.jquery.com/load/)是如何工作的嗎?通過諸如'this。$ el.html(this。$ el.load(url))之類的代碼判斷''我不這麼認爲。你是否意識到我們的代碼試圖一次又一次地獲取相同的模板?儘管瀏覽器可能會緩存它,但這不是一個好習慣。 ['load'](http://api.jquery.com/load/)是異步的。請先閱讀文檔。 –
感謝您的回答。當我的模板位於index.html頁面時,我使用了模板「template」。它在這段代碼中未被使用。 我真的不想加載它,但使用它。我知道如果它在index.html中,但不在其他文件中,該怎麼做。 – BoltMaud