我已经开始使用 Backbone.js,并试图了解路由到特定视图。在我的HTML
我有<a href="#project/1">
标签来呈现项目任务的视图。
询问
单击该链接时,它会将带有 的任务附加id
到DOM
,但是,当单击第二个链接时,它会将该任务附加到前一个链接的下方。我不确定$.empty
View 的最佳实践是否然后调用该show
方法?
我的路由器的片段:
routes: {
'project/:id: 'showtasks'
},
showtasks: function(id) {
Event.trigger('tasks:show', id);
}
任务集合的片段
initialize: function() {
Event.on('tasks:show', this.show, this);
},
show: function() {
var task = this.collection.get(id);
var taskView = new App.Views.Task({ model: task });
this.$el.append(taskView.render().el);
}
该系列
var tasks = new App.Collections.Tasks([
{
id: 1,
title: 'First Task',
content: 'Lots of Content...'
},
{
id: 2,
title: 'Second Task',
content: 'Lots of Content...'
},
{
id: 3,
title: 'Third Task',
content: 'Lots of Content...'
}
]);
var tasksView = new App.Views.Tasks({ collection: tasks });