Last active
May 25, 2016 19:36
-
-
Save mweststrate/0f5eccd35a00df265e69a44b5f29f70c to your computer and use it in GitHub Desktop.
Intercept example
This file contains 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
const theme = observable({ | |
backgroundColor: "#ffffff" | |
}) | |
intercept(theme, "backgroundColor", change => { | |
if (!change.newValue) { | |
// ignore attempts to unset the background color | |
return null; | |
} | |
if (change.newValue.length === 6) { | |
// correct missing '#' prefix | |
change.newValue = '#' + change.newValue; | |
return change; | |
} | |
if (change.newValue.length === 7) { | |
// this must be a properly formatted color code! | |
return change; | |
} | |
throw new Error("This doesn't look like a color at all: " + change.newValue); | |
}) | |
theme.backgroundColor = "acacac" // will become #acacac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment