Sha256: f862fbf31f9b60126efd72cbf3abc0ce907d28cc8c31d18eb9bb7f30bcec06bb

Contents?: true

Size: 1.41 KB

Versions: 3

Compression:

Stored size: 1.41 KB

Contents

(function(_, Backbone) {
  'use strict';
  function Decorator(models, options) {
    this._decoratee = models instanceof Backbone.Collection ? models.models : models;
    this.initialize.call(this, models, options);
  }

  function wrapDecorator(fn) {
    return function() {
      var args = arguments;
      if (_(this._decoratee).isArray()) {
        return _(this._decoratee).map(function(model) {
          return fn.apply(model, args);
        });
      } else {
        return fn.apply(this._decoratee, args);
      }
    };
  }

  _(Decorator).extend({
    extend: function(protoProps, classProps) {
      var proto = _(protoProps).chain().omit('collection', 'constructor', 'initialize', 'model').reduce(function(proto, fn, name) {
        proto[name] = wrapDecorator(fn);
        return proto;
      }, {}).value();

      return _(Backbone.Model.extend.call(this, _(protoProps).extend(proto), classProps)).tap(function(Klass) {
        _(['model', 'collection']).each(function(type) {
          if (protoProps[type]) {
            protoProps[type].prototype.decorator = function() {
              return new Klass(this);
            };
          }
        });
      });
    }
  }, Backbone.extensions && Backbone.extensions.include || {});

  _(Decorator.prototype).extend({
    initialize: function(models, options) {}
  });

  Backbone.extensions = _(Backbone.extensions || {}).extend({Decorator: Decorator});
})(_, Backbone);

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
backbone_extensions-0.0.8 lib/assets/javascripts/backbone_extensions/decorator.js
backbone_extensions-0.0.7 lib/assets/javascripts/backbone_extensions/decorator.js
backbone_extensions-0.0.6 lib/assets/javascripts/backbone_extensions/decorator.js