Sha256: 10454c2414a5cc2d67303b3c2a276e4f4ca76fe2cfb80c1c3540d68058125439

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

pageflow.SubsetCollection = Backbone.Collection.extend({
  mixins: [pageflow.orderedCollection],

  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, collection, options);
      }
    });

    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

3 entries across 3 versions & 1 rubygems

Version Path
pageflow-0.3.0 app/assets/javascripts/pageflow/editor/collections/subset_collection.js
pageflow-0.2.1 app/assets/javascripts/pageflow/editor/collections/subset_collection.js
pageflow-0.2.0 app/assets/javascripts/pageflow/editor/collections/subset_collection.js