我有这个代码:
PageView.js
var PageView = Backbone.View.extend({
events: {
'click .unpublish': 'unpublish'
},
tagName: 'tr',
className: 'pageedit',
template: Handlebars.compile( PageViewTemplate ),
render: function() {
this.$el.html( this.template( this.model ) );
return this;
},
unpublish: function() {
alert('hi');
}
});
PagesView.js
PagesView = Backbone.View.extend({
tagName: 'tbody',
initialize: function() {
this.collection.on('reset', this.render, this);
this.render();
},
template: Handlebars.compile(PagesTemplate),
render: function() {
var countPages = this.collection.at(0).get('count');
_.each(this.collection.at(0).get('pages'), function(model) {
pageView = new PageView({ model: model });
this.$el.append( pageView.render().el );
}, this);
$('.main').html( this.template({ pages: this.$el.html() }) );
this.delegateEvents(this.events);
return this;
},
});
页面.hbs
<td class="pageId">
{{ id }}
</td>
<td class="pagename">
<a href="/pages/{{ alias }}">{{ pagename }}</a>
</td>
<td class="catname">{{ catname }}</td>
<td class="author">{{ author }}</td>
<td>{{ date }}</td>
<td>
{{#if status}}
<a href="#" class="unpublish">
<img src='../{{ siteURL }}assets/img/admin/published.png'>
</a>
{{else}}
<a href="#" class="publish">
<img src='../{{ siteURL }}assets/img/admin/not-published.png'>
</a>
{{/if}}
</td>
<td>
<a href="#" class="intrash">
<img src='../{{ siteURL }}assets/img/admin/delete.png'>
</a>
</td>
页面.hbs
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Catalog</th>
<th>Author</th>
<th>Date</th>
<th>Status</th>
<th>Intrash</th>
</tr>
</thead>
<tbody>
{{{pages}}}
</tbody>
</table>
在 router.js 中创建所有实例:
var pages = new Pages();
pages.fetch().done(function() {
new PagesView({ collection: pages});
});
如果我单击 .unpublish 链接,不会触发 PageView 中的事件。所有视图都呈现良好,但事件不起作用:(可以帮助我吗?