Ensure the `afterLoad` method gets called bound to controller (#658) · hotwired/stimulus@f7bfc35

@@ -13,12 +13,20 @@ class LoadableController extends LogController {

1313

}

14141515

class AfterLoadController extends LogController {

16+

static values = {

17+

example: { default: "demo", type: String },

18+

}

19+1620

static afterLoad(identifier: string, application: any) {

1721

const newElement = document.createElement("div")

1822

newElement.classList.add("after-load-test")

1923

newElement.setAttribute(application.schema.controllerAttribute, identifier)

2024

application.element.append(newElement)

21-

document.dispatchEvent(new CustomEvent("test", { detail: { identifier, application } }))

25+

document.dispatchEvent(

26+

new CustomEvent("test", {

27+

detail: { identifier, application, exampleDefault: this.values.example.default, controller: this },

28+

})

29+

)

2230

}

2331

}

2432

@@ -37,22 +45,26 @@ export default class ApplicationTests extends ApplicationTestCase {

37453846

"test module with afterLoad method should be triggered when registered"() {

3947

// set up an event listener to track the params passed into the AfterLoadController

40-

let data: { application?: any; identifier?: string } = {}

48+

let data: { application?: any; identifier?: string; exampleDefault?: string; controller?: any } = {}

4149

document.addEventListener("test", (({ detail }: CustomEvent) => {

4250

data = detail

4351

}) as EventListener)

445245-

this.assert.equal(data.identifier, undefined)

4653

this.assert.equal(data.application, undefined)

54+

this.assert.equal(data.controller, undefined)

55+

this.assert.equal(data.exampleDefault, undefined)

56+

this.assert.equal(data.identifier, undefined)

47574858

this.application.register("after-load", AfterLoadController)

49595060

// check the DOM element has been added based on params provided

5161

this.assert.equal(this.findElements('[data-controller="after-load"]').length, 1)

52625363

// check that static method was correctly called with the params

54-

this.assert.equal(data.identifier, "after-load")

5564

this.assert.equal(data.application, this.application)

65+

this.assert.equal(data.controller, AfterLoadController)

66+

this.assert.equal(data.exampleDefault, "demo")

67+

this.assert.equal(data.identifier, "after-load")

5668

}

57695870

get controllers() {