Skip to content

Enhancement to provide single declarative place for event listeners #197

Open
@RickButler

Description

@RickButler

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions