Uncaught TypeError: Cannot read property 'navigate' of undefined

Uncaught TypeError: Cannot read property 'navigate' of undefined

所以我根据 backbonejs.org 的教程视频开发了一个简单的 CRUD 程序,代码运行良好。现在我正在尝试在 requirejs 中实现代码,但它在以下代码中显示以下错误:-

define([
'jquery',
'underscore',
'backbone',
'router',
'models/Customers/Customer',
'helper/Serialize'
], function ($, _, Backbone, Router, Customer, Serialize) {

    var CustomerEditView = Backbone.View.extend({

        el: '.page',
        events: {
            'submit .edit-customer-form': 'saveCustomer',
            'click .delete': 'deleteCustomer',

        },

        saveCustomer: function (ev) {
            var customerDetails = $(ev.currentTarget).serializeObject();
            var customer = new Customer();
            customer.save(customerDetails, {
                success: function (customer) {
                    this.router.navigate('', { trigger: true });
                }
            });
            return false;
        },

您可以使用:

customer.save(customerDetails, {
                success: function (customer) {
                    Backbone.history.navigate('', { trigger: true });
                }

如果你想首先使用路由器对象,你必须像

一样初始化它
this.router  = new router();

你可以说 this.router.navigate('', { trigger: true });

在所有视图中创建一个新实例并不是最佳选择,不建议将对象设为全局。 You can use Backbone.history.nvaigate which is alias to router.nvaigate