Backbone View两次触发事件

时间:2012-08-19 12:49:22

标签: javascript backbone.js

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

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方法中有一行记录了单击的输入元素的值。 最初调用视图时,它可以正常工作并登录到控制台。但是在导航到另一个路径并返回之后,单击时会记录输入元素的值两次。可能有什么不对?

1 个答案:

答案 0 :(得分:2)

我认为你只是在Backbone ghost View issue中找到了自己。

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