Created
November 22, 2014 22:21
-
-
Save cdaringe/4cf21c488b0f2365ca09 to your computer and use it in GitHub Desktop.
ampersand // derived value shifted
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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