【问题标题】:Uncaught TypeError: Cannot read property 'navigate' of undefined未捕获的类型错误:无法读取未定义的属性“导航”
【发布时间】:2015-02-27 16:41:24
【问题描述】:

所以我从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;
        },

【问题讨论】:

标签: backbone.js requirejs


【解决方案1】:

你可以使用:

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

【讨论】:

  • 使用Backbone.history.navigate 而不是this.router.navigate 也解决了我的问题。谢谢!
猜你喜欢
  • 2021-12-22
  • 1970-01-01
  • 2018-02-16
  • 2015-01-06
  • 2017-07-26
  • 2019-02-26
  • 2021-12-25
相关资源
最近更新 更多