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

Allow function to be specified as callback #1

Merged
merged 1 commit into from
Mar 5, 2015

Conversation

nathanstitt
Copy link

Backbone events could be written inline.

This is useful for for cases where it doesn't quite make
sense to create a method just for them. Such as:

Backbone.View.extend({
    events:{
       'click button': function(){ this.quack(1) }
     }
    quack: function(number_of_times){

    }

    unrelated_method: function(){
       this.quack(3);
    }
})

If the callback was just 'quack', it would be passed the Event object.

It can also be useful for adding events later in the view's lifecycle by calling
this.eventManager.bind() with a function that has closure scope.

My personal use case for it is to setup a listener on the form when it renders:

render: function(){
        var special_value = calculateHash();
        AV.prototype.render();
        this.eventManager.bind("change magic.input", _.partial( this._onFieldChange, special_value, _  ) );
        return this;
}

I prefer to use the eventManager vs setting up EventListeners on the element, since eventManager handles teardown on remove().

Backbone events could be written inline.

This is useful for for cases where it doesn't quite make
sense to create a method just for them.  Such as:

Backbone.View.extend({
  events:
     'click button': function(){ this.quack(1) }

  quack: function(number_of_times){

  }

  unrelated_method: function(){
    this.quack(3);
  }
})

If the callback was just 'quack', it would be passed the Event object.

It can also be useful for adding events later in the view's lifecycle by calling
this.eventManager.bind() with a function that has closure scope.

This extends the events so they can be used similarly.
@kamilogorek
Copy link

@nathanstitt I also added sensible error if no method has been found to fix both AmpersandJS/ampersand-view#38 and AmpersandJS/ampersand-view#37 in one shot.

#2 – your commit included

@HenrikJoreteg HenrikJoreteg merged commit 169e5d8 into HenrikJoreteg:master Mar 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants