0

I am new to Backbone, and asked to add new behavior in a project that was given to me.

I want to extend Backbone Events in order to add some code in trigger and listenTo functions. I found a similar question but when the solution given doesn't work for me.

//My event emitter

var _ = require('underscore');
var Backbone = require('backbone');

const EVENTS = {
 SYNC: 'SYNC',
 BLACK_BG: 'BLACK_BG',
 PLAY: 'PLAY',
 SCROLL_UP: 'SCROLL_UP',
 SCROLL_DOWN: 'SCROLL_DOWN',
};

class EventEmitter {
 constructor() {
  _.extend(this, Backbone.Events);
  this.events = EVENTS;
 }
}


class PromiseEventEmitter extends EventEmitter{
 constructor() {
  super();
 }

 listenTo(object, events, callback) {
  //My code should reach here  
  super.listenTo(object, events, callback);
 }
}

module.exports = new PromiseEventEmitter();
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Tal Humy
  • 1,197
  • 1
  • 18
  • 41
  • It looks like an [XY problem](https://meta.stackexchange.com/q/66377/254800) to me. What are you trying to achieve with this? Please provide a valid [mcve]. – Emile Bergeron Nov 15 '17 at 18:14
  • I wrote an answer that goes into details on [how to make a good base class with Backbone](https://stackoverflow.com/a/40982556/1218980). – Emile Bergeron Nov 15 '17 at 18:19
  • And in a [lib I open sourced](https://github.com/emileber/backtension/blob/master/backtension.js), I extend the `Events` object of each Backbone classes. – Emile Bergeron Nov 15 '17 at 18:20
  • I will explain what I'm trying to achieve. Lets say I have 3 listeners to an event name 'addNewItem'. I want to enable each listener callback to return a promise, if he is doing something async. Now when I use the trigger function for 'addNewItem' I want to get a promise that will be resolved only when the listenTo functions finished their work. I don't want to change all the listenTo and trigger function to a new functions because it will force me to change to much code. – Tal Humy Nov 16 '17 at 07:29
  • 1
    @EmileBergeron Looks like when more features are added to your project, it becomes..... marionette? :) – T J Nov 16 '17 at 13:11
  • @TJ I would have really liked to use marionette at the time, but a _raw_ Backbone app was already started before I joined... – Emile Bergeron Nov 16 '17 at 15:20
  • @TalHumy please, edit the additional information into your question. Comments are for us to ask (missing) details, the question description is for... the question description! :P – Emile Bergeron Nov 16 '17 at 15:22

0 Answers0