Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated model creation #9

Open
nevf opened this issue Apr 19, 2014 · 4 comments
Open

Automated model creation #9

nevf opened this issue Apr 19, 2014 · 4 comments

Comments

@nevf
Copy link

nevf commented Apr 19, 2014

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

@JohnLouderback
Copy link
Owner

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

@nevf
Copy link
Author

nevf commented Apr 21, 2014

Hi John,
Yes that it's precisely. The returned object would then be used as the model.

@JohnLouderback
Copy link
Owner

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

@nevf
Copy link
Author

nevf commented Apr 22, 2014

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 };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants