Sha256: ae173fa1e237da4e102e62b059815b0e00dad221ce2933ec24f3ae925ef5223a

Contents?: true

Size: 1.38 KB

Versions: 1

Compression:

Stored size: 1.38 KB

Contents

(function(Backbone, $) {
  function Decorator(models, options) {
    var self = this,
        decoratee = models, proto = _(Object.getPrototypeOf(this)).omit('collection', 'constructor', 'initialize', 'model');
    if (models instanceof Backbone.Collection) {
      decoratee =  decoratee.models;
    }
    if (decoratee instanceof Array) {
      _(proto).each(function(fn, name) {
        self[name] = function() {
          var args = arguments;
          return _(decoratee).map(function(model) {
            return fn.apply(model, args);
          });
        };
      });
    } else {
      _(proto).each(function(fn, name) {
        self[name] = function() {
          return fn.apply(decoratee, arguments);
        };
      });
    }
    self.initialize.apply(self, arguments);
  }

  _(Decorator).extend({
    extend: function(protoProps, classProps) {
      var Klass = Backbone.Model.extend.apply(this, arguments);
      if (protoProps.model) {
        protoProps.model.prototype.decorator = function() {
          return new Klass(this);
        };
      }

      if (protoProps.collection) {
        protoProps.collection.prototype.decorator = function() {
          return new Klass(this);
        };
      }

      return Klass;
    }
  }, Backbone.include ? Backbone.include : {});

  _(Decorator.prototype).extend({initialize: $.noop});

  Backbone.Decorator = Decorator;
})(Backbone, jQuery);

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
backbone_extensions-0.0.2 lib/assets/javascripts/backbone_extensions/decorator.js