-
Notifications
You must be signed in to change notification settings - Fork 20
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
.data() sometimes returns incorrect values after a re-render #390
Comments
Testing examples: Template:
View:
DOM change in the browser: |
https://stackoverflow.com/questions/13524107/how-to-set-data-attributes-in-html-elements
So using .data() to set data attributes will not update the HTML element. This might make it more difficult to debug in the browser. |
Will it work if I update data on the new node to sync with the new attributes after the new attributes are set in https://github.com/vecnatechnologies/backbone-torso/blob/master/modules/templateRenderer.js#L101-L104? Something like:
so if we use
if we use
Note: |
@beiyi0207 sorry it took me so long to get back around to this. I think the current behavior is acceptable, but we should probably make it more clear in the documentation that values as data- attrs in the template may not be safe to check if they change on re-render. Maybe just having this issue is enough? |
The jQuery data() method caches accessed values internally and does not update them if the underlying DOM is changed. ("The data- attributes are pulled in the first time the data property is accessed and then are no longer accessed or mutated (all data values are then stored internally in jQuery)." -- https://api.jquery.com/data/). This can result in unexpected behavior if you use attr() or setAttribute() to update the DOM and then try to get the new value using data() (e.g. https://stackoverflow.com/questions/40075669/jquery-data-function-returns-the-wrong-value). Since Torso uses setAttribute internally when re-rendering, using .data() on elements of a view can unexpectedly return incorrect info if the data attributes were changed by the re-render.
We should either fix this so that re-rendering updates the element data, or make it explicit in the documentation that it is not safe to use the data() method in situations where re-rendering changes data attributes.
See: https://github.com/vecnatechnologies/backbone-torso/blob/master/modules/templateRenderer.js#L91-L104
The text was updated successfully, but these errors were encountered: