Comes up periodically that people want to alter the order of behaviors being run or only run a few of them. Instead of modifying Drupal.behaviors object we should have a way of controlling that explicitly (came up in #2362637: Editor.js attach/detach doesn't pass context all the way down). Having this would mean that the behavior system is justified and Drupal can't use events to do it's thing, see #1446166-39: Use JS events instead of Drupal.behaviors.

Introducing a new parameter, behaviorList, and a new function Drupal.behaviorList.

Drupal.attachBehaviors(document, drupalSettings, ['AJAX', 'editor', 'formUpdated', 'escapeAdmin']);

// Override default behavior ordering
Drupal.behaviorList = function (event, trigger) { return myCustomOrderedBehaviorsArray; };

Same for Drupal.detachBehaviors. behaviorList defaults to the whole list of behaviors. This can also be used to alter the order in which behaviors are run.

Beta phase evaluation

@TODO

Reference: https://www.drupal.org/core/beta-changes
Issue category Task
Issue priority Normal
Prioritized changes Main goal of issue is adding flexibility to drupal behaviors management.
  • Allow a supported way to alter behavior order and make the behavior order more predictable by having a Drupal.behaviorList
  • As we get more and more JS on pages, being able to selectively run behaviors (or exclude some) is good.
Disruption API addition, no API break.
Files: 
CommentFileSizeAuthor
#12 core-js-behaviorList-2367655-12.patch4.88 KBnod_
PASSED: [[SimpleTest]]: [PHP 5.4 MySQL] 92,167 pass(es). View

Comments

nod_’s picture

Status: Needs review » Needs work

Umm actually having a Drupal.behaviorList function would be better, it can be overriden by contrib.

nod_’s picture

Status: Needs work » Needs review
FileSize
4.97 KB
PASSED: [[SimpleTest]]: [PHP 5.4 MySQL] 80,979 pass(es). View

Not quite ready yet, doc probably needs work.

nod_’s picture

FileSize
4.98 KB
FAILED: [[SimpleTest]]: [PHP 5.4 MySQL] Unable to apply patch core-js-behaviorList-2367655-3.patch. Unable to apply patch. See the log in the details link for more information. View

Reroll

This would be a solution for the weight issue #1945262: Replace custom weights with dependencies in library declarations; introduce "before" and "after" for conditional ordering, make the after/before in the JS, not in the file ordering.

lauriii’s picture

Status: Needs review » Reviewed & tested by the community

I tested this manually and it does actually work! Also the code changes looks clean. Thanks nod_ for your hero coding on this issue!!

Wim Leers’s picture

Issue tags: +JavaScript
+++ b/core/misc/drupal.js
@@ -18,6 +18,21 @@ if (window.jQuery) {
+   * @param {String} event
...
+   * @param {String} [trigger]
...
+   * @return {Array}

Is this {Something} stuff now the norm/standard/rule?

nod_’s picture

Will be, yes. Once I get the the courage to work on #2182153: [Meta] Document Drupal JavaScript using JSDoc.

nod_’s picture

Wim Leers’s picture

Thanks for both #6 and #7!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 3: core-js-behaviorList-2367655-3.patch, failed testing.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 3: core-js-behaviorList-2367655-3.patch, failed testing.

nod_’s picture

Status: Needs work » Needs review
FileSize
4.88 KB
PASSED: [[SimpleTest]]: [PHP 5.4 MySQL] 92,167 pass(es). View

Reroll after eslint update patch.

Status: Needs review » Needs work

The last submitted patch, 12: core-js-behaviorList-2367655-12.patch, failed testing.

nod_’s picture

Status: Needs work » Needs review

lauriii’s picture

Status: Needs review » Reviewed & tested by the community

Looks good!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 12: core-js-behaviorList-2367655-12.patch, failed testing.

Status: Needs work » Needs review
nod_’s picture

Status: Needs review » Reviewed & tested by the community
alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs issue summary update

This issue is a normal task so we need to outline how it fits within the allowable Drupal 8 beta criteria. Can someone add Drupal 8 beta phase evaluation template to the issue summary.

I've confirmed that no existing usages call attachBehaviors or detachBehaviors with an additional argument.

Manuel Garcia’s picture

Issue summary: View changes

Pasted in the template.

nod_’s picture

Issue summary: View changes
Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs issue summary update +Needs change record
alexpott’s picture

Status: Reviewed & tested by the community » Needs work

I'm not really sure how this issue passes the beta evaluation :( Also we need a CR if this is to be considered for commit anyway.

Going to discuss with the other committers.

nod_’s picture

At least I want Drupal.behaviorList function exposed so we can override it when we need to mess with behavior initialization. The behaviorList parameter is welcome but not required.

Only reason it didn't go through earlier was lack of review, it was ready 6 months ago despite my comment in #2, the additional changes needed to end up with #12 are cosmetic. I'll do a CR this afternoon.

nod_’s picture

Version: 8.0.x-dev » 8.1.x-dev
Status: Needs work » Needs review

Moving to 8.1.x

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Wim Leers’s picture

Issue tags: +Needs tests

We can now write JS tests.