Skip to content

Instantly share code, notes, and snippets.

@cdaringe
Created November 22, 2014 22:21
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 cdaringe/4cf21c488b0f2365ca09 to your computer and use it in GitHub Desktop.
Save cdaringe/4cf21c488b0f2365ca09 to your computer and use it in GitHub Desktop.
ampersand // derived value shifted
// Require the lib
var State = require('ampersand-state');
// Create a constructor to represent the state we want to store
var Salsa = State.extend({
props: {
name: 'string',
spicy: 'integer'
},
derived: {
tears: {
cached: false,
deps: ['spicy'],
fn: function calcTears(){
return (this.spicy/2.0);
}
}
}
});
// Create an instance of our object
var salsa = new Salsa({name: 'jalapeno'});
// watch it
salsa.on('change:spicy', function(obj, val) {
console.log('damn, that\'s some spicy stuff! level ' + this.spicy + ", " + this.tears);
});
// set the value and the callback will fire
salsa.spicy = 5;
console.log(salsa.tears);
salsa.spicy = 6;
console.log(salsa.tears);
salsa.spicy = 7;
console.log(salsa.tears);
salsa.spicy = 8;
console.log(salsa.tears);
/*
damn, that's some spicy stuff! level 5, 2.5
2.5
damn, that's some spicy stuff! level 6, 2.5 // HOLD THE PHONE, shouldn't this be 3?
3
damn, that's some spicy stuff! level 7, 3
3.5
damn, that's some spicy stuff! level 8, 3.5
4
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment