Open nevf opened 10 years ago
Hi Neville,
This is actually a very interesting idea to me. I think I get the gist of what you're saying. I'll put this in as a new feature. To my understanding, GDB would have a method which would be passed a selector string which in turn would return an object representing the data structure based on the mapping in each DOM element's data-bindto
attribute, correct? If I'm missing the mark here a little, please let me know and see if you could spec it out for me with some pseudo-code.
Thanks,
-John
Hi John, Yes that it's precisely. The returned object would then be used as the model.
Hi Neville, Just thinking out loud here, but perhaps it might be beneficial if instead of explicitly being able to generate a model from the DOM, perhaps GDB could optionally allow nonexistent properties and indexes to be created on bound elements. For example you could generate a model in this way:
var newModel={};
GDB({
teacher: newModel
},{ implicitModel: true });//new user option property
GDB.modelAppendFromDOM();//new public method
<input data-bindto="teacher.name" type="text">
<input data-bindto="teacher.email" type="email">
Changes to the bound elements or a call to the modelAppendFromDOM
method would create non existent properties and append them to the model, which in the example's case is blank. Any reference to our newModel
variable will now have the new information attached.
Let me know your thoughts on this.
Thanks, -John
Hi John,
I'd need to try this to comment. One thing though is the ability to pass in a dom node to modelAppendFromDOM() so it only looks at that node and it's descendants. This would let me use data-bindto="teacher.name"
in multiple Bootstrap dialogs for example. But maybe you don't support duplicate data-bindto properties.
Back to your example. In my write-less code world I'd see something more like:
var newModel = GDB.modelFromDOM( $('#mydialog') );
GDB( newModel, options };
Something I've been thinking about recently is the ability to create a model by parsing the specified dom branch. Most of my use for 2 way bindings is with Bootstrap Modal dialogs.
So I'd call a function passing it the root element of the dialog and it would look for
data-bindto="xyz"
attributes and return an object that represented the model.I appreciate that this may only work for simpler models, but that's fine for my use case.
-Neville