dist/monitor-all.js
function(error) {

      // Don't connect the instance monitor if errors
      if (error) {
        return callback(error);
      }

      // Called to disconnect the listeners
      var disconnectListeners = function() {
        logger.info('disconnectLiveSync', t.className, model.toJSON());
        model.off('change', modelListener);
        model.syncMonitor.off('change', monitorListener);
        model.syncMonitor.disconnect();
        model.syncMonitor = null;
      };

      // Client-side listener - for persisting changes to the server
      var modelListener = function(changedModel, options) {
        options = options || {};

        // Don't persist unless the model is different
        if (_.isEqual(JSON.parse(JSON.stringify(model)), JSON.parse(JSON.stringify(model.syncMonitor.get('model'))))) {
          logger.info('modelListener.noChanges', t.className, model.toJSON());
          return;
        }

        // Disconnect listeners if the ID changes
        if (model.get('id') !== modelId) {
          logger.info('modelListener.alteredId', t.className, model.toJSON());
          return disconnectListeners();
        }

        // Persist changes to the server (unless the changes originated from there)
        if (!options.isSyncChanging) {
          logger.info('modelListener.saving', t.className, model.toJSON());
          model.save();
        }
      };

      // Server-side listener - for updating server changes into the model
      var monitorListener = function(changedModel, options) {

        // Don't update unless the model is different
        var newModel = model.syncMonitor.get('model');
        if (_.isEqual(JSON.parse(JSON.stringify(model)), JSON.parse(JSON.stringify(newModel)))) {
          logger.info('monitorListener.noChanges', t.className, newModel);
          return;
        }

        // Disconnect if the model was deleted or the ID isn't the same
        var isDeleted = (_.size(newModel) === 0);
        if (isDeleted || newModel.id !== modelId)  {
          logger.info('modelListener.deleted', t.className, newModel);
          disconnectListeners();
        }

        // Forward changes to the model (including server-side delete)
        var newOpts = {isSyncChanging:true};
        if (isDeleted) {
          logger.info('modelListener.deleting', t.className, newModel);
          model.clear(newOpts);
        } else {
          // Make sure the model is set to exactly the new contents (vs. override)
          logger.info('modelListener.setting', t.className, newModel);
          model.clear({silent:true});
          model.set(newModel, newOpts);
        }
      };

      // Connect the listeners
      model.on('change', modelListener);
      model.syncMonitor.on('change', monitorListener);

      // Send back the initial data model
      logger.info('connectInstanceMonitor.done', t.className, model.toJSON());
      callback(null, model.syncMonitor.get('model'));
    }
function(error) {

      // Don't connect the instance monitor if errors
      if (error) {
        return callback(error);
      }

      // Called to disconnect the listeners
      var disconnectListeners = function() {
        logger.info('disconnectLiveSync', t.className, model.toJSON());
        model.off('change', modelListener);
        model.syncMonitor.off('change', monitorListener);
        model.syncMonitor.disconnect();
        model.syncMonitor = null;
      };

      // Client-side listener - for persisting changes to the server
      var modelListener = function(changedModel, options) {
        options = options || {};

        // Don't persist unless the model is different
        if (_.isEqual(JSON.parse(JSON.stringify(model)), JSON.parse(JSON.stringify(model.syncMonitor.get('model'))))) {
          logger.info('modelListener.noChanges', t.className, model.toJSON());
          return;
        }

        // Disconnect listeners if the ID changes
        if (model.get('id') !== modelId) {
          logger.info('modelListener.alteredId', t.className, model.toJSON());
          return disconnectListeners();
        }

        // Persist changes to the server (unless the changes originated from there)
        if (!options.isSyncChanging) {
          logger.info('modelListener.saving', t.className, model.toJSON());
          model.save();
        }
      };

      // Server-side listener - for updating server changes into the model
      var monitorListener = function(changedModel, options) {

        // Don't update unless the model is different
        var newModel = model.syncMonitor.get('model');
        if (_.isEqual(JSON.parse(JSON.stringify(model)), JSON.parse(JSON.stringify(newModel)))) {
          logger.info('monitorListener.noChanges', t.className, newModel);
          return;
        }

        // Disconnect if the model was deleted or the ID isn't the same
        var isDeleted = (_.size(newModel) === 0);
        if (isDeleted || newModel.id !== modelId)  {
          logger.info('modelListener.deleted', t.className, newModel);
          disconnectListeners();
        }

        // Forward changes to the model (including server-side delete)
        var newOpts = {isSyncChanging:true};
        if (isDeleted) {
          logger.info('modelListener.deleting', t.className, newModel);
          model.clear(newOpts);
        } else {
          // Make sure the model is set to exactly the new contents (vs. override)
          logger.info('modelListener.setting', t.className, newModel);
          model.clear({silent:true});
          model.set(newModel, newOpts);
        }
      };

      // Connect the listeners
      model.on('change', modelListener);
      model.syncMonitor.on('change', monitorListener);

      // Send back the initial data model
      logger.info('connectInstanceMonitor.done', t.className, model.toJSON());
      callback(null, model.syncMonitor.get('model'));
    }
function ( error ) { // Don't connect the instance monitor if errors if ( error ) { return callback ( error ) ; } // Called to disconnect the listeners var disconnectListeners = function ( ) { logger . info ( 'disconnectLiveSync' , t . className , model . toJSON ( ) ) ; model . off ( 'change' , modelListener ) ; model . syncMonitor . off ( 'change' , monitorListener ) ; model . syncMonitor . disconnect ( ) ; model . syncMonitor = null ; } ; // Client-side listener - for persisting changes to the server var modelListener = function ( changedModel , options ) { options = options || { } ; // Don't persist unless the model is different if ( _ . isEqual ( JSON . parse ( JSON . stringify ( model ) ) , JSON . parse ( JSON . stringify ( model . syncMonitor . get ( 'model' ) ) ) ) ) { logger . info ( 'modelListener.noChanges' , t . className , model . toJSON ( ) ) ; return ; } // Disconnect listeners if the ID changes if ( model . get ( 'id' ) !== modelId ) { logger . info ( 'modelListener.alteredId' , t . className , model . toJSON ( ) ) ; return disconnectListeners ( ) ; } // Persist changes to the server (unless the changes originated from there) if ( ! options . isSyncChanging ) { logger . info ( 'modelListener.saving' , t . className , model . toJSON ( ) ) ; model . save ( ) ; } } ; // Server-side listener - for updating server changes into the model var monitorListener = function ( changedModel , options ) { // Don't update unless the model is different var newModel = model . syncMonitor . get ( 'model' ) ; if ( _ . isEqual ( JSON . parse ( JSON . stringify ( model ) ) , JSON . parse ( JSON . stringify ( newModel ) ) ) ) { logger . info ( 'monitorListener.noChanges' , t . className , newModel ) ; return ; } // Disconnect if the model was deleted or the ID isn't the same var isDeleted = ( _ . size ( newModel ) === 0 ) ; if ( isDeleted || newModel . id !== modelId ) { logger . info ( 'modelListener.deleted' , t . className , newModel ) ; disconnectListeners ( ) ; } // Forward changes to the model (including server-side delete) var newOpts = { isSyncChanging : true } ; if ( isDeleted ) { logger . info ( 'modelListener.deleting' , t . className , newModel ) ; model . clear ( newOpts ) ; } else { // Make sure the model is set to exactly the new contents (vs. override) logger . info ( 'modelListener.setting' , t . className , newModel ) ; model . clear ( { silent : true } ) ; model . set ( newModel , newOpts ) ; } } ; // Connect the listeners model . on ( 'change' , modelListener ) ; model . syncMonitor . on ( 'change' , monitorListener ) ; // Send back the initial data model logger . info ( 'connectInstanceMonitor.done' , t . className , model . toJSON ( ) ) ; callback ( null , model . syncMonitor . get ( 'model' ) ) ; }
Called when done connecting
Called when done connecting