Example #1
0
Dispatcher.register(function(action) {
  switch(action.actionType) {
    case Constants.CREATE:
      store.create(action.contact);
      store.emitChange();
      break;

    case Constants.DESTROY:
      store.destroy(action.email);
      store.emitChange();
      break;

    case Constants.RECEIVE_ALL:
      _contacts = action.contacts;
      store.emitChange();
      break;

    case Constants.RECEIVE_CREATED:
      // Remove existing copy
      store.destroy(action.contact.email);
      // Add new
      store.create(action.contact);
      // emit change event to notify listeners
      store.emitChange();
      break;

    default:
      // no op
  }
});
 self.read(object[options.idProperty], function(err, entity) {
   if (err) {
     return callback(err)
   }
   if (entity) {
     // We found the object so update
     self.update(object, callback)
   } else {
     // We didn't find the object so create
     self.create(object, callback)
   }
 })
 function createOrUpdate(object, callback) {
   if (typeof object[options.idProperty] === 'undefined') {
     // Create a new object
     self.create(object, callback)
   } else {
     // Try and find the object first to update
     self.read(object[options.idProperty], function(err, entity) {
       if (err) {
         return callback(err)
       }
       if (entity) {
         // We found the object so update
         self.update(object, callback)
       } else {
         // We didn't find the object so create
         self.create(object, callback)
       }
     })
   }
 }
Example #4
0
TodoDispatcher.register(function(action){
    console.log(action.type);

    switch(action.type){
        case TodoConstants.update: {
            console.log('dispatcher triggered');
            TodoStore.update(action.content);
            break;
        }
        case TodoConstants.create: {
            console.log('dispatcher triggered');
            TodoStore.create(action.content);
            break;
        }
        case TodoConstants.remove: {
            console.log('dispatcher remove');
            TodoStore.remove(action.content);
            break;
        }
    }
})
Example #5
0
module.exports = EventEmitter.create({

  /**
    @description  constructor
  */
  __init__: function() {

    Object.defineProperty(this, "handlers", {

      value: {}

    });

    this._initListeners();

  },


  /**
    @description  creates and starts an event handler
    @param        {string} event
    @param        {function} listener
    @return       object
  */
  on: function(event, listener) {

    var eventHandler = createEventHandler(this, event, listener);
    eventHandler.start();
    return eventHandler;

  },


  /**
    @description  creates and starts a single event handler
    @param        {string} event
    @param        {function} listener
    @return       object
  */
  once: function(event, listener) {

    var eventHandler, boundListener;

    boundListener = function() {
      this.removeListener(event, boundListener);
      listener.apply(null, arguments);
    }.bind(this);

    eventHandler = createEventHandler(this, event, boundListener);
    eventHandler.start();
    return eventHandler;

  },


  /**
    @description  stops and destroys all registered handlers
  */
  destroy: function() {

    forEach(this.handlers, function(handler){
      handler.stop();
      handler.destroy();
    });

  },


  //  private

  /**
    @description  initialises all handlers specified by the subscribers property
  */
  _initListeners: function() {

    forEach(this.subscribers || {}, function(methodName, event) {

      if(typeof this[methodName] !== "function") {
        throw ReferenceError.spawn(methodName + ": is not a method on this object");
      }

      this.handlers[event] = this.on(event, this[methodName].bind(this));

    }, this);

  }


});
Example #6
0
 process.nextTick(function() {
   that.create(spec.args);
 });