我得到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