Sha256: 14630877690f3d6301011f49e29331b18b1e5e3644c9ff222ae311293b2284bc

Contents?: true

Size: 1.46 KB

Versions: 9

Compression:

Stored size: 1.46 KB

Contents

pageflow.SubsetCollection = Backbone.Collection.extend({
  constructor: function(options) {
    var adding = false;

    options = options || {};

    this.filter = options.filter || function(item) { return true; };
    this.parent = options.parent;
    this.parentModel = options.parentModel;

    delete options.filter;
    delete options.parent;

    this.model = this.parent.model;
    this.comparator = options.comparator || this.parent.comparator;

    this.listenTo(this.parent, 'add', function(model, collection, options) {
      if (!adding && this.filter(model)) {
        this.add(model, options);
      }
    });

    this.listenTo(this.parent, 'remove', function(model) {
      this.remove(model);
    });

    this.listenTo(this, 'add', function(model, collection, options) {
      adding = true;
      this.parent.add(model);
      adding = false;
    });

    if (options.watchAttribute) {
      this.listenTo(this.parent, 'change:' + options.watchAttribute, function(model) {
        if (this.filter(model)) {
          this.add(model);
        }
        else {
          this.remove(model);
        }
      });
    }

    this.listenTo(this, 'sort', function() {
      this.parent.sort();
    });

    Backbone.Collection.prototype.constructor.call(this, this.parent.filter(this.filter), options);
  },

  clear: function() {
    this.parent.remove(this.models);
    this.reset();
  },

  url: function() {
    return this.parentModel.url() + _.result(this.parent, 'url');
  }
});

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
pageflow-0.11.4 app/assets/javascripts/pageflow/editor/collections/subset_collection.js
pageflow-0.11.3 app/assets/javascripts/pageflow/editor/collections/subset_collection.js
pageflow-0.11.2 app/assets/javascripts/pageflow/editor/collections/subset_collection.js
pageflow-0.11.1 app/assets/javascripts/pageflow/editor/collections/subset_collection.js
pageflow-0.11.0 app/assets/javascripts/pageflow/editor/collections/subset_collection.js
pageflow-0.10.0 app/assets/javascripts/pageflow/editor/collections/subset_collection.js
pageflow-0.9.2 app/assets/javascripts/pageflow/editor/collections/subset_collection.js
pageflow-0.9.1 app/assets/javascripts/pageflow/editor/collections/subset_collection.js
pageflow-0.9.0 app/assets/javascripts/pageflow/editor/collections/subset_collection.js