Sha256: 03822b41326ac9c77a23e6444af2f4a6c853202cf255a952908b8b9cd02e1291

Contents?: true

Size: 1.93 KB

Versions: 10

Compression:

Stored size: 1.93 KB

Contents

pageflow.transientReferences = {
  initialize: function() {
    this.transientReferences = {};
    this.pendingReferences = {};
  },

  getReference: function(attribute, collection) {
    return this.transientReferences[attribute] ||
      collection.get(this.get(attribute));
  },

  setReference: function(attribute, record) {
    this._cleanUpReference(attribute);
    this._setReference(attribute, record);
    this._listenForReady(attribute, record);
  },

  unsetReference: function(attribute) {
    this._cleanUpReference(attribute);
    this.unset(attribute);
  },

  _setReference: function(attribute, record) {
    if (record.isNew()) {
      this.transientReferences[attribute] = record;
      this.set(attribute, null);
      this._setIdOnceSynced(attribute, record);
    }
    else {
      this.set(attribute, record.id);
    }
  },

  _setIdOnceSynced: function(attribute, record) {
    record.once('change:id', function() {
      delete this.transientReferences[attribute];
      this.set(attribute, record.id);
    }, this);
  },

  _listenForReady: function(attribute, record) {
    if (!record.isReady()) {
      this.pendingReferences[attribute] = record;

      this.listenTo(record, 'change:state', function() {
        if (record.isReady()) {
          this._cleanUpReadyListener(attribute);
          this.trigger('change');
        }
      });
    }
  },

  _cleanUpReference: function(attribute) {
    this._cleanUpSaveListener(attribute);
    this._cleanUpReadyListener(attribute);
  },

  _cleanUpSaveListener: function(attribute) {
    if (this.transientReferences[attribute]) {
      this.stopListening(this.transientReferences[attribute], 'change:id');
      delete this.transientReferences[attribute];
    }
  },

  _cleanUpReadyListener: function(attribute) {
    if (this.pendingReferences[attribute]) {
      this.stopListening(this.pendingReferences[attribute], 'change:state');
      delete this.pendingReferences[attribute];
    }
  }
};

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
pageflow-0.4.0 app/assets/javascripts/pageflow/editor/models/mixins/transient_references.js
pageflow-0.3.0 app/assets/javascripts/pageflow/editor/models/mixins/transient_references.js
pageflow-0.2.1 app/assets/javascripts/pageflow/editor/models/mixins/transient_references.js
pageflow-0.2.0 app/assets/javascripts/pageflow/editor/models/mixins/transient_references.js
pageflow-0.1.0 app/assets/javascripts/pageflow/editor/models/mixins/transient_references.js
pageflow-0.0.5 app/assets/javascripts/pageflow/editor/models/mixins/transient_references.js
pageflow-0.0.4 app/assets/javascripts/pageflow/editor/models/mixins/transient_references.js
pageflow-0.0.3 app/assets/javascripts/pageflow/editor/models/mixins/transient_references.js
pageflow-0.0.2 app/assets/javascripts/pageflow/editor/models/mixins/transient_references.js
pageflow-0.0.1 app/assets/javascripts/pageflow/editor/models/mixins/transient_references.js