2 ms·
This is fine for scalar binding of simple values - a property mapping to a value - but what about vector binding, where you have a list of items, and need the U
by barrkel 11y ago
This is fine for scalar binding of simple values - a property mapping to a value - but what about vector binding, where you have a list of items, and need the UI to reflect insertions, deletions and sorts?
And what about enabled state, display state, or more complicated binding that changes the structure of a DOM node - e.g. converting the icon on a button to fa-cirle-o-notch while an asynchronous operation is occurring?
It might be fine for extremely simple needs, but if you're going with a UI which dynamically reflects your model and application state, you should IMO go all the way. When done right (which usually means as functional, in the programming sense, as possible), a binding setup greatly reduces the amount of thinking you need to do to keep the UI in sync.
(I'm probably missing something and the library is more capable than it looks; on a closer examination, I do see it doing some kind of vector binding, but I don't think it can do e.g. heterogeneous elements, where the HTML per element differs depending on the model.)
- tedmiston 11y agoI think you're correct about its intended use case being different. I used Twitter's Mustache compiler, Hogan.js, in a similar fashion for a tiny app once. Though I suppose I should highlight the latter being client-side templating, as opposed to one-way data binding.
- daliwali 11y agoauthor here - getting iteration and nesting to work was the hardest part, and I don't think the basic example on the homepage illustrates this too well. the way it works is that every value gets cast into an array for rendering purposes if it isn't already. so in case of an array of objects, you don't need to explicitly define that you expect an array, it'll just render `n` objects with nested bindings. to give an example, on the homepage there's an array that is rendered, `["Tall", "Grande", "Venti"]`. if a new array is set, for example, `["Tall", "Grande"]` (without "Venti"), it will re-render based on if the values at the indices changed, so in this case it would just remove the dom node for "Venti". another cool thing is that with the mount function, you can either return nothing so that the node that was specified will be appended, or return a completely different node based on the value, which enables heterogeneous elements in a list. the mount function also gives you an opportunity to mutate a node in other ways than just setting the text/value/checked/whatever, such as setting a classname i.e. `node.className = "fa fa-hourglass"`. then there's the unmount function but 99% of the time won't be needed because dom nodes that aren't in the live tree and don't have a reference will just get GC'd. it should be entirely possible to build a web app with Simulacra.js using a single bound object, a single template, and describing all possible bindings in one go. it's up to the developer to add whatever convoluted architectures are hip at the moment (stores, reducers, actions, what have you).