From 48743c7dee3b8bcebe253e5fee589511832f1f67 Mon Sep 17 00:00:00 2001 From: Ryan Date: Fri, 11 Sep 2020 17:57:33 +0800 Subject: [PATCH] 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 --- build/js/TodoList.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/build/js/TodoList.js b/build/js/TodoList.js index f63fa5199..47da5e0f0 100644 --- a/build/js/TodoList.js +++ b/build/js/TodoList.js @@ -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]() } }) }