Skip to content

Instantly share code, notes, and snippets.

@aprock
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aprock/a008634a6b7e215bb7a3 to your computer and use it in GitHub Desktop.
Save aprock/a008634a6b7e215bb7a3 to your computer and use it in GitHub Desktop.
exploring stampit samples
// playing with the bar example from https://github.com/ericelliott/stampit
// added a concept of specials
'use strict';
var stampit = require('stampit');
// Some more privileged methods, with some private data.
// Use stampit.mixin() to make this feel declarative:
var availability = stampit().init(function () {
var isOpen = false; // private
return stampit.mixin(this, {
open: function open () {
isOpen = true;
return this;
},
close: function close () {
isOpen = false;
return this;
},
isOpen: function isOpenMethod () {
return isOpen;
}
});
});
// Here's a mixin with public methods, and some state:
var membership = stampit({
methods: {
addMember: function (member) {
this.members[member.name] = member;
return this;
},
getMember: function (name) {
return this.members[name];
}
},
refs: {
members: {}
}
});
var menu = stampit({
methods: {
addSpecial: function (special) {
this.specials[special.name] = special;
return this;
},
getSpecial: function (name) {
return this.specials[name];
}
},
refs: {
specials: {}
}
});
// Let's set some defaults:
var defaults = stampit().refs({
name: 'The Saloon',
specials: {Whisky: {name: 'Whisky'},
Gin: {name: 'Gin'},
Tequila: {name: 'Tequila'}}
});
// Classical inheritance has nothing on this. No parent/child coupling. No deep inheritance hierarchies.
// Just good, clean code reusability.
var bar = stampit.compose(availability, membership, menu, defaults);
// Note that you can override references on instantiation:
var myBar = bar({name: 'Moe\'s'});
// Silly, but proves that everything is as it should be.
myBar.addMember({name: 'Homer'}).addSpecial({name: 'Duff Beer'}).open().getMember('Homer');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment