Skip to content

Instantly share code, notes, and snippets.

@charlesjolley
Created January 31, 2011 02:12
Show Gist options
  • Save charlesjolley/803538 to your computer and use it in GitHub Desktop.
Save charlesjolley/803538 to your computer and use it in GitHub Desktop.
SC.Enumerable = SC.Mixin.create({
forEach: SC.Mixin.required(),
init: SC.Mixin.concat(),
destroy: SC.Mixin.concat(),
map: function(f, ctx) {
var ret = [];
if (!ctx) ctx = this;
this.forEach(function(x) { ret.push(f.call(ctx,x)); });
return ret;
},
contains: function() { ... }
});
// add more properties to a trait later on
SC.Enumerable.mixin({
filter: function() { .. }
});
// ensures that all SC.Array's are also enumerables
SC.Array = SC.Mixin.create(SC.Enumerable, {
objectAt: function() { ... },
contains: function() { ... }
});
// wot! both Array and Enumerable define contains. Use the without
// operation:
SC.Array = SC.Mixin.create(SC.Enumerable.without('contains'), {
});
// but what if we still want the Enumerable contains?
SC.Array = SC.Trait.create(
SC.Enumerable.rename({ contains: '_enumerableContains' }), {
// array properties here...
});
// you can apply a mixin to any object instance.
SC.Array.apply(Array.prototype);
// You can also build objects the same way
SC.Set = SC.Object.extend(SC.Enumerable, { ... });
myView = SC.View.create(SC.Bordered, { .. })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment