So, how can we combine these two immensely powerful libraries to do something interesting? Or at least vaguely interactive.
Let’s see if we can use D3 to draw some rectangles, described in a Knockout view-model, which we can modify using D3 or by altering the data itself.
There is some overlap in functionality here: KO enables us to generate DOM content based on data, as does D3. In theory we could use a KO template to generate fragments of SVG markup for each of our view models. But that would involve generating the elements long-hand and we wouldn’t have the association between view and view-model.
Now, we need a way of getting the KO data fed to D3 at the right time: when it’s updated.
One of the first things to notice is that if we observe the array of rects, we only see changes to the array, not to its elements. In other words, we only know when items are added or removed, not when elements properties are changed. Seeing as we need to know when a rectangle’s position changes, we’ll need to do some more work.
- See more at: http://www.voyce.com/index.php/2013/06/23/dynamic-d3-with-knockout-js/#sthash.IDxcsa5W.dpuf
Your new post is loading...