| Project: | Drupal core |
| Version: | 8.x-dev |
| Component: | javascript |
| Category: | task |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
| Issue tags: | html5, JavaScript, sprint |
Issue Summary
HTML5 allows us to embed metadata via the data-* attribute. I would like to open a discussion on how we might be able to use the data attribute in Drupal core, or if we even should.
Some relevant reading...
http://dev.w3.org/html5/spec/Overview.html#attr-data
http://html5doctor.com/html5-custom-data-attributes/
http://plugins.jquery.com/project/customdata
List of issues where the use of the data- attribute is relevant: (from comment #12)
#1217030: Clean up the CSS for Overlay module
#1216980: Clean up the CSS for Dashboard module
#1217032: Clean up the CSS for Poll module
#675446: Use jQuery UI Autocomplete
Possible uses:
#1473760: Use data-* to check modules dependencies before submit
#1573020: Use data-* for CSS and JS files list
#1589176: Use data-* to store #states api informations
Comments
#1
I would definitely welcome removing most of our current Javascript settings and embed those directly in the DOM. Most of the settings (AJAX settings, Tabledrag configuration, etc.) target precise elements anyway.
#2
The data attributes are definitely something we should use. Drupal.settings can get pretty hairy at times. Switching to data attributes would improve page loading speed, clean up the markup, and reduce the amount of code required.
jQuery 1.6 greatly improves the performance of .data() retrieval, so postponed for #1085590: Update jQuery UI to the latest in the git repository.
Changing to a meta issue so that we can split it into different parts of core... AJAX settings, Tabledrag configuration, etc, like Damien mentioned.
#3
I wouldn't necessarily agree with converting most, but general usage of data attributes is absolutely a must. Whether data attributes make sense vs. Drupal.settings needs analysis per use-case. Non-ID selectors being the prime example, of course. Markup may also be replaced via #ajax and may use the identical settings afterwards.
#4
Subscribe.
#5
#6
There are some cases where .once() could be switched with .data(). The benefit would be speed as the DOM wouldn't have to re-render the elements, the downside is that we wouldn't be able to select the newly processed elements with CSS.
#7
Would love to see some progress here. Adding it to the current sprint.
#8
I have been using data- attributes to handle urls in the sliding navigation on my latest mobile navigation prototype. http://nav3.d8mux.org/admin
#9
So anyone has any ideas for this?
I'm not totally convince that data-* should completely replace Drupal.settings. But maybe we can give developers a simple way of getting their data-* attributes, even though technically is already pretty simple.
Maybe this is just documenting a new way of doing things.
Actually I have no idea :-p just starting the conversation all over again.
#10
I think that Drupal.settings is a nice convention. Perhaps we can make it an interface that other javascript implements. That implementing code can decide show it's going to store data (localStorage, data- syntax, ajax to key-value storage mechanism).
If we're talking about a persistence layer for our javascript, should the ideal solution be generalized enough for this?
#11
I fully agree that some things needs to get out of Drupal.settings. Some data structure in settings are workaround to be able to use $.extend() and they are not very pretty (I'm looking at you ajaxPageState).
I'm not sure we want an abstraction over localStorage or data-, they have different use-case and have a API that's not too bad, nothing like the DOM from a couple of years ago, but I don't know, it could be good to have. At least I'm sure about using data- attributes (like most people here).
So I guess the next step to get this going is list all use-cases in core and decide case by case what we're going to do. Anyone up for it?
#12
User Stories:
List of complex widgets that may qualify as needing data- to drive state-based logic
#13
About form elements i don't think that'll be possible because I think FAPI needs to precess things on the server too. That would be cool if that was possible though.
states.jsuse fields attributes to store conditions.(edit)
From sun @ IRC
Indeed #1473760: Use data-* to check modules dependencies before submit.
#14
Edit module: data- attributes implementation
In the Edit module, I'm using
data-attributes to add metadata to existing output (specifically: rendered fields and entities). Often, the ID attribute is already set. Even more often, classes are already set.So, what I do is: 1) never add an ID attribute; 2) add classes for selecting (I cannot imagine
jQuery('[data-edit-id]')to be faster thanjQuery('.edit-field')); 3) add data attributes for describing, in my case:data-edit-idwhich is<entity type>:<entity ID>:<field name>anddata-edit-field-label.I still use
Drupal.settings, but only for "global settings", such as the URL where subforms can be generated (which is built based on thedata-edit-id).Best practice?
AFAICT, the best practice rule regarding ID+class vs. data- attributes is very simple, and as follows:
#15
Over at #675446: Use jQuery UI Autocomplete, we're switching to the data- attribute for the autocomplete path.