Skip to content

Instantly share code, notes, and snippets.

@mweststrate
Last active May 25, 2016 19:36
Show Gist options
  • Save mweststrate/0f5eccd35a00df265e69a44b5f29f70c to your computer and use it in GitHub Desktop.
Save mweststrate/0f5eccd35a00df265e69a44b5f29f70c to your computer and use it in GitHub Desktop.
Intercept example
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