Network.Models.FeedItem = Backbone.Model.extend({
isPost: function() {
return this.get('template') === 'post';
}
})
Network.Models.Post = Backbone.Model.extend({
urlRoot: '/p',
hasImageInDetails: function() {
if(this.get('details').match(/<img[^<]*>[\w\d]*<\/img>|<img[^\/]*\/>/i)) {
return true
}
}
)}
模板 feed_items/post.jst.eco
<% if @model.hasImageInDetails(): %>
has image
<% end %>
查看
Network.Views.FeedItemView = Backbone.View.extend({
initialize: function() {
var self = this;
this.template = JST[this.path()];
},
render: function() {
var self = this;
$(this.el).html(this.template({ model: this.model }));
if (this.model.isPost()) {
this.questionView = new Network.Views.FeedItems.Post({
model: this.model,
el: this.$el,
parent: this
});
}
}
});
Network.Views.FeedItems.Post = Backbone.View.extend({
initialize: function() {
this.render();
},
render: function() {
var self = this;
}
});
但我得到一個錯誤:
Uncaught TypeError: Object [object Object] has no method 'hasImageInDetails'
你能還包括調用'eco.render'的方法的代碼?它看起來像模板中的@ model'變量可能不是Backbone Model實例。 – jevakallio
當然我包括郵政視圖 – tomekfranek
我沒有看到該示例中的任何位置呈現模板。它可能在'TagsRoot'視圖中被調用? – jevakallio