未激发 Backbone.js 模型事件

ua4mk5z4  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(162)

我使用backbone和requirejs,我在一个全局应用程序对象上触发一个事件,并设置应用程序何时启动一个句柄,但由于某种原因,该事件没有被捕获。
main.js

require([
    "app",
    "router",
    "modules/facebookLogin"
],

function(app, Router,FacebookLogin) {

  // Define your master router on the application namespace and trigger all
    // navigation from this instance.
    app.router = new Router();
    app.currentFacebookSession = new FacebookLogin.Model();
    app.currentFacebookSession.on({
        'facebook:unauthorized': function () {
            //app.router.navigate('', true);
        },
        'facebook:disconnected': function () {
            //app.router.navigate('', true);
        },
        'facebook:connected': function () {
            //app.router.navigate('events', true);
            console.log('EVENTS');
        }
    });

  // Trigger the initial route and enable HTML5 History API support, set the
  // root folder to '/' by default.  Change in app.js.
  Backbone.history.start({ pushState: true, root: app.root });
    //Init facebook

...
模型是facebookLogin.js

// Facebookuser module
define(["app", "facebookSDK"],

// Map dependencies from above array.
function(app,FB) {

  // Create a new module.
  var FacebookLogin = app.module();

  // Default Model.
  FacebookLogin.Model = Backbone.Model.extend({

      initialize: function (attributes, options) {
          options || (options = {});
          this.options = _.defaults(options, this.defaultOptions);
          this.facebookInitialize();
          _.bindAll(this, 'onLoginStatusChange');
         
          FB.Event.subscribe('auth.authResponseChange', this.onLoginStatusChange);
      },
.....

   onLoginStatusChange: function (response) {
          if (this._loginStatus === response.status) return false;

          var event;

          if (response.status === 'not_authorized') {
              event = 'facebook:unauthorized';
          } else if (response.status === 'connected') {
              event = 'facebook:connected';
              if (this.options.autoFetch === true) this.fetch();
          } else {
              event = 'facebook:disconnected';
          }

          this.trigger(event, this, response);
          this._loginStatus = response.status;
      },

在调试时,它会进入触发器,并使用以下参数运行触发器。

this.trigger(event, this, response);

这=

child
_changes: Object
_escapedAttributes: Object
_pending: Object
_previousAttributes: Object
attributes: Object
changed: Object
cid: "c0"
id: ""
onLoginStatusChange: function bound() {
options: Object
__proto__: Surrogate

事件= "facebook:connected"
而回应是正确的对象
我现在替换了app.Router.nagigate来用日志调试这个问题,但是它从来没有发生过,知道为什么吗?

41zrol4v

41zrol4v1#

因此,我使用的 Backbone.js 版本并不是允许通过on函数进行多事件绑定的版本。
已更新至1.0.0,现在可以使用。

相关问题