Sha256: fe7f8faeb31a83cef376d867db080fcb817f3576d1f04243a2b92e409acfc9f1

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 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(fnName) {
    var Klass = this;
    return function() {
      var args = arguments;
      if (_(this._decoratee).isArray()) {
        return _(this._decoratee).map(function(model) {
          return (Klass.fn[fnName]).apply(model, args);
        });
      } else {
        return (Klass.fn[fnName]).apply(this._decoratee, args);
      }
    };
  }

  _(Decorator).extend({
    extend: function(protoProps, classProps) {
      var proto = _(protoProps)
          .chain()
          .omit('collection', 'constructor', 'initialize', 'model'),
          wrapped = proto.reduce(function(proto, fn, name) {
            return (proto[name] = wrapDecorator.call(this, name)) && proto;
          }, {}, this).value();
      this.fn = proto.value();
      return _(Backbone.Model.extend.call(this, _(protoProps).extend(wrapped), 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});
}).call(this, _, Backbone);

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
backbone_extensions-0.0.10 lib/assets/javascripts/backbone_extensions/decorator.js
backbone_extensions-0.0.9 lib/assets/javascripts/backbone_extensions/decorator.js