Small fix for TodoList plugin (#3013)

Properly attach the event listener to the correct selector and fix on the options passed on in the JQuery API. However, a new bug comes from this fix when both data-widget and Jquery API is set where the event listener will be triggered twice
This commit is contained in:
Ryan 2020-09-11 17:57:33 +08:00 committed by GitHub
parent 16b4d30d31
commit 48743c7dee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -64,7 +64,7 @@ class TodoList {
// Private
_init() {
const $toggleSelector = $(SELECTOR_DATA_TOGGLE)
const $toggleSelector = this._element
$toggleSelector.find('input:checkbox:checked').parents('li').toggleClass(CLASS_NAME_TODO_LIST_DONE)
$toggleSelector.on('change', 'input:checkbox', event => {
@ -77,15 +77,18 @@ class TodoList {
static _jQueryInterface(config) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
const _options = $.extend({}, Default, $(this).data())
if (!data) {
data = new TodoList($(this), _options)
$(this).data(DATA_KEY, data)
data = $(this).data()
}
const _options = $.extend({}, Default, typeof config === 'object' ? config : data)
const plugin = new TodoList($(this), _options)
$(this).data(DATA_KEY, typeof config === 'object' ? config : data)
if (config === 'init') {
data[config]()
plugin[config]()
}
})
}