Skip to content

Instantly share code, notes, and snippets.

@thgreasi
Last active September 20, 2017 17:28
Show Gist options
  • Select an option

  • Save thgreasi/926e13f0a02d58b5164eca93c51a0386 to your computer and use it in GitHub Desktop.

Select an option

Save thgreasi/926e13f0a02d58b5164eca93c51a0386 to your computer and use it in GitHub Desktop.

Revisions

  1. thgreasi revised this gist Apr 9, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions firebaseCheckpointArray.js
    Original file line number Diff line number Diff line change
    @@ -3,8 +3,8 @@
    https://jsfiddle.net/katowulf/1dfyz2rq/
    Discussed in:
    firebase/angularfire#687
    angular-ui/ui-sortable/#421
    https://github.com/firebase/angularfire/issues/687
    https://github.com/angular-ui/ui-sortable/issues/421
    */
    angular.module('firebase.checkpointArray', ['firebase'])
    .factory('firebaseCheckpointArray', function($firebaseArray) {
  2. thgreasi created this gist Apr 9, 2016.
    67 changes: 67 additions & 0 deletions firebaseCheckpointArray.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    /*
    Implemented by katowulf at:
    https://jsfiddle.net/katowulf/1dfyz2rq/
    Discussed in:
    firebase/angularfire#687
    angular-ui/ui-sortable/#421
    */
    angular.module('firebase.checkpointArray', ['firebase'])
    .factory('firebaseCheckpointArray', function($firebaseArray) {
    return $firebaseArray.$extend({
    checkpoint: function() {
    this._checkpointIds = this._buildIds();
    },

    saveChanges: function() {
    if (!this._checkpointIds) {
    throw new Error('No checkpoint declared. Can\'t save yet. You may need to wait until $loaded() completes.');
    }
    var oldIds = this._checkpointIds;
    var currentIds = this._buildIds();
    var recs = this.$list;
    var ref = this.$ref();

    this._checkpointIds = null;

    //console.log('oldIds', oldIds);
    //console.log('currentIds', currentIds);

    // look for removed records
    angular.forEach(oldIds, function(pos, key) {
    if (!currentIds.hasOwnProperty(key)) {
    //console.log('removed', key);
    ref.child(key).remove();
    delete this._indexCache[key];
    }
    }, this);

    // look for moved or added records
    angular.forEach(currentIds, function(pos, key) {
    var rec = this.$list[pos];
    if (pos !== oldIds[key] || pos !== rec.$priority) {
    //console.log('saving', rec.$id, oldIds[key], pos, rec.$priority);
    rec.$priority = pos;
    this._indexCache[key] = pos;
    this.$save(rec);
    }
    }, this);
    },

    _buildIds: function() {
    var ids = {};
    var ref = this.$ref();
    angular.forEach(this.$list, function(rec, i) {
    // to simplify prioritizing and moving things around
    // assign all records an id right away, even if they
    // have not been saved to the server before
    // they will be added when calling saveChanges()
    if (!rec.$id) {
    rec.$id = ref.push().key();
    }
    ids[rec.$id] = i;
    });
    return ids;
    }
    });
    });