Backbone 路由函数 goto 不存在

Backbone router function goto doesn't exist

我正在处理我在 codepen 上找到的示例。该示例是关于创建后退按钮功能的。我遇到的问题是当页面加载时 App.Router 不知道 goto 函数,它说它是 Uncaught TypeError: this.goto is not a function。我已经看过这个例子很多次来尝试解决这个问题,但我似乎根本无法让它工作。这个例子在 codepen and my working example is here

我的问题是路由器不知道我放在里面的 goto 函数。

非常感谢您的帮助

代码:

<!DOCTYPE html>
<html>
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.1/backbone-min.js"></script>
    <script type="text/javascript">
        (function() {

            window.App = {
                Models: {},
                Views: {},
                Extensions: {},
                Forms: {},
                Controllers: {},
                Router: null,
                linkClicked: false,
                routes: [],
                backDetected : false,
                previousFragment : null,

                init: (route) => {
                    new App.Router();
                    this.instance = new App.Views.App();

                    Backbone.history.start();

                }
            }

            App.Router = Backbone.Router.extend({
                routes: {
                  '': 'home'
                },
                execute: function(callback, args) {
                    var self = this,
                        answer = $.Deferred(),
                        route = Backbone.history.getFragment();

                    if (route === '') {
                        route = 'home';
                    }

                    App.backDetected = false;

                    if (App.routes[App.routes.length - 2] === route && !App.linkClicked) {

                        App.backDetected = true;
                        App.routes.pop();

                        if (App.routes[App.routes.length - 1] === route) {
                            App.routes.pop();
                        }

                    }

                    if (App.routes[App.routes.length - 1] !== route) {
                        App.routes.push(route);
                    }

                    _.delay(function() {
                        // this is super hacky but need a delay as global event on links
                        // takes ages to execute
                        App.linkClicked = false;
                    }, 500);

                    answer.promise().then(function() {

                        App.dirty = false;

                        window.onbeforeunload = null;

                        if (callback) {
                            callback.apply(self, args);
                        } else {
                            console.log('no callback');
                        }
                    });

                    if (App.dirty) {
                        console.log('app is dirty');

                        window.onbeforeunload = (function(_this) {

                        })(this);

                    } else {
                        answer.resolve();
                    }
                },
                initialize: function(options) {
                    var self = this;
                    this.options = options || {};
                },
                goto: function(view, params) {
                    console.log('goto');
                    console.log(App.instance);
                },
                home: (query) => {
                    console.log('home');
                    let view = new App.Views.Home();
                    this.goto(view); // doesn't work
                }           
            });

            App.Extensions.View = Backbone.View.extend({
                goto: function(view) {
                    console.log('showing view');
                }
            });

            App.Views.App = App.Extensions.View.extend({

            })

            App.Views.Home = App.Extensions.View.extend({

            });

        })();



        $(function() {
          //new App.Router();

          window.App.init();



        }); 
    </script>
    <title></title>
</head>
<body>

</body>
</html>

home 函数正在使用绑定 this 上下文的箭头函数。因此 window 对象可能是被绑定的(而不是路由器)并且 goto 不存在。将其更改为普通函数即可修复它。

home: function(query) {
  console.log('home');
  let view = new App.Views.Home();
  this.goto(view); // doesn't work
}