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
#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: Follow-up: Use data-* to store #states api informations
#2180921: [META] Use data attributes rather than classes wherever possible
Issues pending because of this issue:
#1305882: drupal_html_id() considered harmful; remove ajax_html_ids to use GET (not POST) AJAX requests
Comment | File | Size | Author |
---|---|---|---|
#27 | drupalSettingsajax.png | 33.98 KB | andyceo |
#2 | 5683836956_3074f66856.jpg | 77.67 KB | RobLoach |
Comments
Comment #1
Damien Tournoud CreditAttribution: Damien Tournoud commentedI 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.
Comment #2
RobLoachThe 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 to jQuery UI 1.9.
Changing to a meta issue so that we can split it into different parts of core... AJAX settings, Tabledrag configuration, etc, like Damien mentioned.
Comment #3
sunI 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.
Comment #4
JacineSubscribe.
Comment #5
sunComment #6
RobLoachThere 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.
Comment #7
JacineWould love to see some progress here. Adding it to the current sprint.
Comment #8
LewisNymanI have been using data- attributes to handle urls in the sliding navigation on my latest mobile navigation prototype. http://nav3.d8mux.org/admin
Comment #9
ericduran CreditAttribution: ericduran commentedSo 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.
Comment #10
cosmicdreams CreditAttribution: cosmicdreams commentedI 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?
Comment #11
nod_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?
Comment #12
cosmicdreams CreditAttribution: cosmicdreams commentedUser Stories:
List of complex widgets that may qualify as needing data- to drive state-based logic
Comment #13
nod_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.js
use fields attributes to store conditions.(edit)
From sun @ IRC
Indeed #1473760: Use data-* to check modules dependencies before submit.
Comment #13.0
nod_Revise description summary to point to branching issues from this meta issue.
Comment #13.1
nod_add js-related issues
Comment #14
Wim LeersEdit 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-id
which 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:
Comment #15
RobLoachOver at #675446: Use jQuery UI Autocomplete, we're switching to the data- attribute for the autocomplete path.
Comment #15.0
RobLoachadd data-drupal-states
Comment #16
mortendk CreditAttribution: mortendk commentedfrom the css & markup perspective this would be wonderfull :)
we could then remove the js- prefix from the css, so a theme could pack whatever css it wants without unfortunately breaking drupal - an epic win !
+3 for this
Comment #16.0
mortendk CreditAttribution: mortendk commentedg
Comment #16.1
cosmicdreams CreditAttribution: cosmicdreams commentedRemoved reference to Dashboard CSS cleanup as that's not in Drupal core anymore.
Comment #17
catchComment #18
catchComment #19
martin107 CreditAttribution: martin107 commentedAdded major bug fix pending
Comment #20
catchComment #21
catchComment #22
LewisNymanComment #23
LewisNymanComment #24
mitokens CreditAttribution: mitokens as a volunteer commentedComment #25
nod_Actual name proposed and used for a while already.
Comment #26
andypostsuppose related is affected by this change
Comment #27
andyceo CreditAttribution: andyceo commentedFound interesting bug:
When you have an ajax form on the page, and when some ajax operation happened, Drupal send new generated form elements IDs to the drupalSettings.ajax array. See HTML IDs are now randomized for AJAX responses
Suppose we need some kind of client-side invalidation? Does this problem will be solved by using data attributes instead of global variable?
Comment #28
nod_Ah.
It looks bad but does it trigger wired bugs on the page? Because if it does it might be more than a major bug…
Comment #29
andyceo CreditAttribution: andyceo commentedNo, I didn't find any bugs related to this, on frontnd or backend.
But if you have a heavily used ajax form (for example ajax filter, like exposed views filters), your page in browser will be larger and larger, and finally crashed.
Comment #30
nod_Ok good, not critical then.
We need a new major issue for this. Can you open one please? I have a few ideas to solve it.
Comment #31
andyceo CreditAttribution: andyceo commentedSure. Please follow: #2561619: Drupal Ajax objects and settings grows endlessly
Comment #32
xjmThis issue was marked as a beta target for the 8.0.x beta, but is not applicable as an 8.1.x beta target, so untagging.
This could potentially be implemented in a minor with BC, so moving to 8.2.x.
Comment #35
almaudoh CreditAttribution: almaudoh commentedComment #39
andypost