我已經開始使用Backbone.js,並試圖繞過路由到特定視圖。在我的HTML
中,我有<a href="#project/1">
標籤來呈現項目任務的視圖。Backbone路由器和視圖
查詢
當點擊該鏈接,它追加與id
到DOM
任務,然而,在點擊第二個鏈接時,其附加以前下這一任務。我不確定其最佳做法是否$.empty
查看然後調用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 });
你想要它做什麼(一次只顯示一個)?當觸發事件時調用'$ .empty'就可以了。你說這是你的'Collection'代碼,但它看起來好像是一個Backbone'View'(因爲'Collection'沒有關聯元素('el''))。代碼看起來是一個'TaskViewViewer'?我不知道'id'來自'show'功能的位置......可能只是沒有顯示出來? – WiredPrairie 2013-03-18 21:47:51
對不起,是的,這是我的'收集意見'。我會用集合更新我的問題。最終,我想嵌套集合,所以多個項目與一個項目相關聯。 – ashley 2013-03-18 21:54:10
您可以清空附加到其中的html的任何事件,或者您可以調用它。$ el.html(taskView.render( ).el)來代替當前的內容。 – Kalpers 2013-03-19 00:33:18