Let's say I want to create a multi-color picker with svelte, maybe to let the user choose a foreground color and a background color. My data model that looks like this:
{
foreground: {
r: 100,g:100,b:100
},
background: {
r: 200,g:200,b:200
}
};
So my app.js is
import AppUI from './App.html';
import { Store } from 'svelte/store.js';
const defaultData = {
foreground: {
r: 100,g:100,b:100
},
background: {
r: 200,g:200,b:200
}
};
const store = new Store(defaultData);
window.store = store; // useful for debugging!
store.onchange(() => console.log('something changed'));
var app = new AppUI({
target: document.querySelector( '#main' ),
store
});
export default app;
Then I can build an RGBSelector component to reuse:
<input type="range" min=0 max=255 step=1 bind:value=data.r/>{{data.r}}
<input type="range" min=0 max=255 step=1 bind:value=data.g/>{{data.g}}
<input type="range" min=0 max=255 step=1 bind:value=data.b/>{{data.b}}
And my App.html is pretty simple:
foreground:
<RGBSelector bind:data=$foreground/>
background:
<RGBSelector bind:data=$background/>
<script>
import RGBSelector from './RGBSelector.html';
export default {
components: {
RGBSelector
}
};
</script>
This seems to work, mostly. The two-way binding in the range inputs is working (the labels update), and the store is even being updated (verified by inspecting store._state in the console). So I believe the bind keywords in the RGBSelector are passing the change up to where they're declared in the App, which in turn is binding them to the store.
Trouble is, the store.onchange handler is not firing. Can anyone see what I'm doing wrong?
Full example: https://glitch.com/edit/#!/nonstop-hourglass