骨干模型事件没有解雇

时间:2013-04-22 17:35:34

标签: backbone.js javascript-events requirejs backbone-events

我正在使用带有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

event = "facebook:connected"

并且响应是正确的对象

我现在更换app.Router.nagigate来调试日志问题,但它永远不会发生,任何想法为什么?

1 个答案:

答案 0 :(得分:0)

事实证明,我使用的骨干版本不是通过on函数允许多个事件绑定的版本。

已更新至1.0.0,现在可以使用。