Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

views: events getting called before initialize in 1.2.*? #3704

Closed
builtbylane opened this issue Jul 7, 2015 · 7 comments
Closed

views: events getting called before initialize in 1.2.*? #3704

builtbylane opened this issue Jul 7, 2015 · 7 comments

Comments

@builtbylane
Copy link

just updated backbone to 1.2.* from 1.1.2 and I'm running into an issue in my views where the events block is being called before the initialize block. Was this an intentional change? I didn't see this in the changelog.

Had to lock into 1.1.2 since this change is breaking my app. (I conditionally extend the events hash depending on options set within the initialize block.)

@builtbylane builtbylane changed the title events getting called before initialize? views: events getting called before initialize in 1.2.*? Jul 7, 2015
@jridgewell
Copy link
Collaborator

Dupe of #3620, changed in #3060, documentation in #3640:

Views now always delegate their events in setElement. You can no longer modify the events hash or your view's el property in initialize.

@akre54
Copy link
Collaborator

akre54 commented Jul 7, 2015

Turn your events hash into a method or use delegate in your initialize method.

@builtbylane
Copy link
Author

so turn it into a method and call it from initialize?

@jridgewell
Copy link
Collaborator

No, you can either turn events into a method, or call #delgate in #initialize:

var View = Backbone.View.extend({
  events: function() {
    return {
      'click span': 'onClickSpan',
      'click': 'onClick'
    };
  }
});

// OR

var View = Backbone.View.extend({
  initialize: function() {
    this.delegate('click', 'span', this.onClickSpan);
    this.delegate('click', this.onClick);
  }
});

@builtbylane
Copy link
Author

thank @jridgewell @akre54. So I've been doing that, but I was relying on initialize being called before the events method:

var View = Backbone.View.extend({
  events: function() {
    var eventsHash = {};
    if (this.doClickSpan) {
      _.extend(eventsHash, { 
        'click span' : 'onClickSpan'
      });
    }
    return eventsHash;
  },
  initialize: function(opts) {
    var o = opts || {};
    this.doClickSpan = o.doClickSpan
  }
});

new View({doClickSpan: true});

this pattern no longer works in Backbone 1.2.*

@builtbylane
Copy link
Author

@jridgewell, sorry to be a nuisance, but what was the motivation for making these changes?

@jridgewell
Copy link
Collaborator

See #3060 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants