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

Enhancement to provide single declarative place for event listeners #197

Open
RickButler opened this issue Feb 24, 2018 · 0 comments
Open

Comments

@RickButler
Copy link
Collaborator

I often find myself with multiple places inside my more complicated views where I start and stop listening for events. I've put some serious thought into it, and would like add centralized declarative event registration to ampersand-view.

e.g.

ampersandEvents: [
  {
    object: this.something,
    callback: this.doSomething,
    name: 'all'
  }
],

which is essentially -> this.listenTo(this.something, 'all', this.doSomething);

In addition, I would like to add some more behavior that I haven't fully mapped out. I would like to add the option of registering the event listener once an event has occurred e.g.

this.once('change:rendered', function () { this.listenTo(this.something, 'all', this.doSomething); });

Something like:

ampersandEvents: [
  {
    start: 'change:rendered',
    stop: 'change:remove',
    object: this.something,
    callback: this.doSomething,
    name: 'all'
  }
],

It may need conditional start and stop, which I don't particularly like. I'm open to suggestions on a better way to do it.

ampersandEvents: [
  {
    start: 'change:rendered',
    startCondition: function (model, value) {  return value; }, // only starts if rendered is true
    stop: 'change:remove',
    stopCondition: function () { return true; }, // this is just to show the stop condition function.
    object: this.something,
    callback: this.doSomething,
    name: 'all'
  }
],

If we don't want to include this in ampersand-view I understand, I will likely release it as an extension in that case.

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

No branches or pull requests

1 participant