-
Notifications
You must be signed in to change notification settings - Fork 159
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
Why do we need to call _isElUserEditable in function copyViewValuesToModel() #167
Comments
@kiinoo - honestly, I forget why I included _isElUserEditable but I know I didn't add it there just for fun. Let me think about it and see if I can recall why it exists... Please don't bother with a pull request. I need to refresh my memory... |
@theironcook thanks for your quick reply. I'm trying to get value from a <TD>, but failed because it has no 'contenteditable‘ attribute. |
OK, I remember I added these guards because the code was crashing for elements that were not editable in some situations. Can you please show me your bindings? How are you setting 'contenteditable' for elements in your bindings? |
No, I did not set 'contenteditable', I just want to bind values in TD back to property of a model. below is my code: |
|
OK, thank you for the detailed explanation and examples. This function will not work in a lot of situations...
The problem is that you are only looking directly under the parentEl and you are automatically binding to each and every one of the children and you are ignoring the bindings. That completely bypasses most of the ModelBinder's architecture. The ModelBinder is designed to use it's bindings to define scope - what should be bound etc. This might included nested DOM elements or some DOM elements should be ignored etc. You said you are trying to bind the values of a 'td' back into your model. I'm guessing it's because you are populating the 'td' values on the server and you want to bind those values into your model. Normally I would strongly disagree with this approach - I'd want to bootstrap or fetch your backend data and populate a backbone model. Then bind that model to your view. But I don't know your project or your constraints so maybe it's not possible to do that. So, if you have to continue with this approach here is what I would suggest. Your example with 'td' elements suggests you have a table and you are populating several models into a table. If that is the case, it makes things messy because you need to iterate through each 'tr' and populate a model. Normally the CollectionBinder is used for this type of situation but it can't for this situation. If your table only has a single row and you are populating a single model, you should still be using a ModelBinder with a bindings hash defined. Something like this...
This is basically pseudo code - I'm not sure if I messed up any syntax. But it should get you close to what you need. Or you can just keep your existing shims. |
Why need I call this function?
I have a web page opened in browser, I want to reformat the page. What I'm trying is to build the models from existing html elements and then use these models to drive the page.
What is my question?
I'm trying to call that function, but model is not populated with values in view.
I read the doc, we can see:
but this is actually inaccurate, we need to set attribute 'contenteditable' to true for each html element that is bound to a property of model, unless the element is 'input', 'select' or 'textarea'. see below code:
so the "copying values from view to model" is blocked by _isElUserEditable() function.
I wonder why we need that? can we remove it?
What is my trial solution?
I created a function called copyViewValuesToModel() in ModelCollectionBinder (see below), so we can bind an html element to a collection in which the model is bind to child of the html element. But again the "copying values from view to model" is blocked by _isElUserEditable() function.
does the new function (copyViewValuesToModel) above make sense? if yes, I will send a pull request.
thanks.
The text was updated successfully, but these errors were encountered: