Backbone.History.extend({loadUrl:...})对于有效路由返回false [英] Backbone.History.extend( { loadUrl: ...} ) returning false for valid routes

查看:93
本文介绍了Backbone.History.extend({loadUrl:...})对于有效路由返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试扩展Backbone.History.loadUrl()以捕获404错误:

I'm trying to extend Backbone.History.loadUrl() to catch 404 errors:

var History = Backbone.History.extend({
        loadUrl: function() {
            var match = Backbone.History.prototype.loadUrl.apply(this, arguments);
            if (!match) {
                console.log('route not found');
            }
            return match;
        }
    }); 

(Backbone.history = new History).start();

这基于此处建议的解决方案: https://github.com /jashkenas/backbone/issues/308#issuecomment-9482299 .

This is based on the solution suggested here: https://github.com/jashkenas/backbone/issues/308#issuecomment-9482299.

我遇到的问题是,当我在有效路由上调用(Backbone.history = new History).start()时,它返回false.但是,当我在同一路径上调用Backbone.history.start()时,它将返回true.

The problem I'm encountering is that when I call (Backbone.history = new History).start() on a valid route, it returns false. However, when I call Backbone.history.start() on the same route, it returns true.

当我将调试器添加到扩展的loadUrl方法时,match设置为false.

When I add a debugger to the extended loadUrl method, match is set to false.

关于导致差异的任何想法?

Any ideas about what's causing the discrepancy?

推荐答案

应该可以使用.

var oldLoadUrl = Backbone.History.prototype.loadUrl;
_.extend(Backbone.History.prototype, {
  loadUrl : function () {
    var matched = oldLoadUrl.apply(this, arguments);
    if (!matched) {
      console.log('route not found');
    }
    return matched;
  }
});

这篇关于Backbone.History.extend({loadUrl:...})对于有效路由返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆