javascript - Backbone.js - 未捕获类型错误 : Cannot call method 'receivedEvent' of undefined

标签 javascript jquery backbone.js

我在我的项目中使用backbone.js。在浏览器上运行项目时出现此错误。

Uncaught TypeError: Cannot call method 'receivedEvent' of undefined

我的代码

define(['jquery', 'underscore', 'backbone', 'model/username/usernameModel', 'text!modules/home/homeViewTemplate.html'], function($, _, Backbone, usernameModel, homeViewTemplate) {
    var HomeView = Backbone.View.extend({
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    },
        events: {
            "click #nextButton": "next",
            "click #resetButton": "resetPassword"
        },
        next: function(event) {
            window.location.replace("#login");
        },
        resetPassword: function() {
            window.location.replace("#reset");
        },
        render: function() {
            this.$el.html(homeViewTemplate);
        }
    });
    return HomeView;
});

我在这里定义了我的应用程序

require.config({
    //path mappings for module names not found directly under baseUrl
    paths: {
        jquery:     'vendor/jqm/jquery_1.7_min',
        jqm:     'vendor/jqm/jquery.mobile-1.1.0', 
        underscore: 'vendor/underscore/underscore_amd',
        backbone:   'vendor/backbone/backbone_amd',
        text:       'vendor/require/text',
        cordova:       'vendor/phoneGap/cordova',
        wikitudePlugin:       'vendor/wikitude/WikitudePlugin',
        plugin:    'plugin',
        initOptions:   'initOptions',
        main:   'main',
        messages:   'messages',
        templates:  '../templates',
        modules:    '../modules',
        model:       '../model'
    },
    shim: {
        'backbone': {
            deps: ['underscore', 'jquery'],
            exports: 'Backbone'
        },
        'jquery': {
            exports: '$'
        },
        'jqm': {
            deps: ['jquery'],
            exports: '$'
        },
        'initOptions': {
            deps: ['jquery'],
            exports: '$'
        },
        'main': {
            deps: ['jquery'],
            exports: '$'
        },
        'messages': {
            deps: ['jquery'],
            exports: '$'
        },
        'cordova': {
            deps: ['jquery'],
            exports: '$'
        },
        'wikitudePlugin': {
            deps: ['jquery'],
            exports: '$'
        },
        'underscore': {
            exports: '_'
        },
    }

});

//1. load app.js, 
//2. configure jquery mobile to prevent default JQM ajax navigation
//3. bootstrapping application
define(['app','jqm-config'], function(app) {
    $(document).ready(function() {
      console.log("DOM IS READY");// Handler for .ready() called.
    });    
    app.initialize();
});

为什么?

最佳答案

app 未在您的 HomeView 中定义。它不知道它是什么(因此,“无法在未定义时调用方法 receiveEvent”。

有多种方法可以为监听器绑定(bind)上下文。您可以让您的 bindEvents 函数执行以下操作:

bindEvents: function() {
  document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},

并更改 onDeviceReady 以适用于 app 的任何实例,因此只需使用 this:

onDeviceReady: function() {
  this.receivedEvent('deviceready');
},

关于javascript - Backbone.js - 未捕获类型错误 : Cannot call method 'receivedEvent' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21566934/

相关文章:

javascript - 如何在 JavaScript 中获取还包含秒数的当前日期?

javascript - 当鼠标进入 div 时显示顶部菜单栏

javascript - Grunt - 无法使用 grunt-contrib-less 将 less 编译为 css

javascript - 没有设置回调的主干保存

javascript - Google Map API BackBoneJS 无法读取 null 的属性 'offsetWidth'

Javascript:函数绑定(bind)不适用于调用者

javascript - 重复 500 次时 jQuery 追加花费太多时间

jquery - jScrollPane:Webkit中拖动jspDrag位时水平滚动条向左移动

javascript - 悬停工具提示显示详细 jquery

javascript - angularjs 中的主干样式模型?