【问题标题】:Backbone: before routeBackbone:在路由之前
【发布时间】:2013-10-24 14:45:48
【问题描述】:

谷歌中有很多主题,但任何完整且有效的示例。

如何包装路由器的导航,以便我可以检查条件并在需要时取消导航?

附:我看到 _.wrap 的想法,覆盖 Backbone.Router.prototype.route 等,但没有完整的例子。 我也看到了backbone-route-filter,但不知道如何将它集成到我的应用程序中。

【问题讨论】:

  • 那些配置的路由会触发一些js代码,然后你可以执行任何js验证并使用window.location.replace()将站点重定向到另一个url。这对你有用吗?
  • 重定向有一个问题,如果你重定向到一个 url,这反过来会导致 return 陷入僵局。小心点。

标签: javascript backbone.js backbone-routing


【解决方案1】:

为此,我扩展了 Backbone.Router。希望我的路由器能帮到你。 我添加了一个属性 toEvaluate 与路由的先决条件,并添加了 evaluateRoutesFn 和函数,该函数将在导航某些路由之前评估它们。

我的路线示例可能是:

var MySubRouter = Backbone.ModuleRoute.extend({
      //hasPermissions is static
      //if you want a function of an instance you can use a function instead

      evaluateRoutesFn: SomeView.hasPermissions,

      toEvaluate: {
          routeA: {
              modulePreconditions: [SomeView.CONDITION_A, ...]
          }
      },

      routeA: function(param1, param2) {
          //the route to navigate with preconditions
      },

有权限的方法返回真假或错误。

var MyView = ModuleView.extend({}, {
    CONDITION_A: "conditionA",
    hasPermissions: function (condition, properties) {
          switch (condition) {
              case MyView.CONDITION_A:
                  return app.ConditionA;
              default:
                  return true;
          }
    }
});

更重要的是ModuleRoute。在这里,您有了我的 moduleRoute 的基础。我添加了更多的东西。就像错误控制一样。我在我的应用程序的每个模块中保存了所有当前路线......等等。你可以添加任何你需要的东西。真的很有用。

  Backbone.ModuleRoute = Backbone.Router.extend({
      evaluateRoutesFn: null,
      toEvaluate: null,

      constructor: function (prefix, options) {
          this._finalRoutes = {};
          Backbone.SubRoute.prototype.constructor.call(this, prefix, options);
      },

      onRoutes: function (route) {
          var aps = Array.prototype.slice;
          var args = aps.call(arguments);
          args.shift();
          var routeName = this.routes[route];

          var evalResult = true;
          try {
              var toEval = this.toEvaluate && this.toEvaluate[routeName];
              if (toEval) {
                  evalResult = this.evalPreconditions(toEval.modulePreconditions, args);
                  //more future preconditions
              }
          }
          catch (err) {
              evalResult = false;
          }

          if (evalResult)
              try {
                  this._finalRoutes[route].apply(this, args);
              }
              catch (err) {
                  window.history.back(); //go back, error control...
              }
          else
              window.history.back(); //no permissions, go back
      },

      evalPreconditions: function (preconds, args) {
          if (!this.evaluateRoutesFn) {
              throw "WARNING: Evaluate routes function must be overriden to evaluate them. " +
                    "Don't assign toEvaluate if you won't do it. The evaluation has been skipped.";
          }
          if (!preconds)
              return true;

          var evalResult = true;
          for (var i = 0, len = preconds.length; i < len; i++) {
              evalResult = this.evaluateRoutesFn(preconds[i], args);
              if (!evalResult) {
                  throw "ERROR: The precondition is not truth.";
                  break;
              }
          }
          return evalResult;
      },

      route: function (route, name, callback) {
          this._finalRoutes[route] = (!callback) ? this[name] : callback;
          var that = this;
          callback = function (path) {
              return function () {
                  var aps = Array.prototype.slice;
                  var args = aps.call(arguments);
                  args.unshift(path);
                  that.onRoutes.apply(that, args);
              };
          } (route);
          return Backbone.Router.prototype.route.call(this, route, name, callback);
      }
  });

【讨论】:

    【解决方案2】:

    您可以尝试覆盖 Backbone.history.route:

    var bbhRoute = Backbone.history.route;
    Backbone.history.route = function(route, callback) {
      return bbhRoute.call(Backbone.history, route, function(fragment) {
        // if (decide if you want to prevent navigation) {
        //    window.history.back(); // to ensure hash doesn't change
        //    return;
        // }
    
        // if you want to let it happen:
        return callback.apply(this, arguments);
      });
    };
    

    【讨论】:

    • 在路由器初始化方法中
    • 最好放在路由器外面
    猜你喜欢
    • 1970-01-01
    • 2014-12-26
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    相关资源
    最近更新 更多