2

我有一个通过路由初始化的主干视图,但是当我导航到另一条路由并通过链接再次返回到前一个路由时,视图中的事件被触发两次 这是我的路由器

define(['underscore','backbone','views/projects/view_project',
    'views/projects/project_tasks','views/projects/project_milestones',
    'views/projects/project_tasklists','views/projects/project_documents'
    ],
    function( _,Backbone,ProjectTasks,ProjectMilestones,
        ProjectTasklists,ProjectDocuments) {

    var ProjectRouter = Backbone.Router.extend({
        initialize: function(projects) {
            if(projects) {
                this.projects = projects;
            }
        },
        //url routes mapped to methods
        routes: {
            "project/:id":"get_project",
            "project/:id/milestones":"get_project_milestones",
            "project/:id/tasks":"get_project_tasks",
            "project/:id/tasklists":"get_project_tasklists",
            "project/:id/documents":"get_project_documents"
        },

        get_project: function(id) {
            UberERP.UI.loadpage("#project-view");
            var project_view = new ProjectView(this.projects,id);
        },

        get_project_tasks: function(id) {
            UberERP.UI.loadpage("#project-tasks-view");
            var project_tasks_view = new ProjectTasks(id,this.projects);
        },

        get_project_tasklists: function(id) {
            UberERP.UI.loadpage("#project-tasklist-view");
            var project_tasks_view = new ProjectTasklists(id,this.projects);
        },

        get_project_milestones: function(id) {
            UberERP.UI.loadpage("#project-milestones-view");
            var project_milestones_view = new ProjectMilestones(id,this.projects);
        },

        get_project_documents: function(id) {
            UberERP.UI.loadpage("#project-documents-view");
            var project_documents_view = new ProjectDocuments(id,this.projects);
        }
    });

    return ProjectRouter;
});

和视野中的狙击手

events: {
          "click input[name=task]":"select_task",
          "click a.remove-icon":"remove_task",
          "click td.view-task":"view_task",
          "click #project-tasks-table .sort-by-status":"sort_by_status",
          "click #project-tasks-table .group-filter-btn":"sort_by_task_list"
        },

select_task: function( event ) {
            var el = $(event.currentTarget);
            row = el.parent('td').parent('tr');
            console.log(el.val());
            if(row.hasClass('active')) {
                row.removeClass('active');
            }
            else {
                row.addClass('active');
            }
        }

我在 select_task 方法中有一行记录单击的输入元素的值。最初调用视图时,它可以正常工作并记录到控制台。但是在导航到另一条路线并返回后,单击时输入元素的值会被记录两次。有什么问题?

4

1 回答 1

2

我认为您只是发现自己处于Backbone ghost View 问题的中间。

当您从一条路线移动到另一条路线时,请尝试删除取消绑定所有视图。

于 2012-08-19T12:55:48.217 回答