Skip to content

Instantly share code, notes, and snippets.

@ralphsmith80
Last active December 13, 2015 17:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ralphsmith80/4948316 to your computer and use it in GitHub Desktop.
Save ralphsmith80/4948316 to your computer and use it in GitHub Desktop.
Model events as relationships

This is my first take on providing functionality for modeling events as relationships within our views.

listenTo: function(subject, evt, observerCallback) {
  var relationshipIndex = this.get('relationshipIndex') || [],
      relationship = {
        subject: subject,
        evt: evt,
        observerCallback: observerCallback
      };
 
  subject.on(evt, observerCallback);
  this.set('relationshipIndex', relationshipIndex.push(relationship));
},
stopListening: function() {
  var relationshipIndex = this.get('relationshipIndex') || [];
      _.each(relationshipIndex, function(relationship) {
        var subject = relationship.subject, evt = relationship.evt,
          observerCallback = relationship.observerCallback;
          subject.off(evt, observerCallback);
      });
},

This article describes modeling events as relationships with Backbone and Javascript.

TODO:

  • add funcitonality for monitoring if the subject is changed out with a different subject.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment