不更改路由位置backline.js的触发器方法

Trigger method without change route location backbone.js

本文关键字:js 触发器 方法 backline 位置 路由      更新时间:2023-09-26

我对backbone.js history有问题。

当用户单击链接时,我会停止传播,并且我希望在不更改位置的情况下调用与href匹配的方法。

$(document).on("click", "a[href^='/']", function(event){
  event.preventDefault();
  event.stopPropagation();
  Router.goToIntraRoute($(event.target).attr("href"));
});

在主干router:中

var Router = Backbone.Router.extend({
  routes: {
    "user/:id": "openProfile"
  },
  // Changes the location
  modifyRoute: function(route){
    this.navigate(route, {trigger: true, replace: false});
  },
  // Doesn't change the location
  goToIntraRoute: function(route){
    // URL modified
    this.navigate(route, {trigger: false, replace: false});
  }
});

我希望方法goToIntraRoute不修改我的url,但触发方法。。。有人能帮我吗?

假设您不希望新的"页面"出现在历史记录中,您可以直接调用路由处理程序。为了让你开始,这里有一个简化的例子:

goToIntraRoute: function(route){
    if (this.routes[route]) {
        this.routes[route]();
    }
}

要实际使用该片段,您需要适当地管理上下文(this)。此外,如果您的路由包括通配符或正则表达式,那么您将不得不从参数中显式解析它们,因为您将不依赖路由器。