Skip to content

Commit

Permalink
Add ready callback to API
Browse files Browse the repository at this point in the history
  • Loading branch information
wheeyls committed Jul 7, 2019
1 parent f1c2070 commit f98b72c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@g2crowd/widget",
"version": "1.0.1",
"version": "1.1.0",
"description": "Rails-friendly jquery plugin wrapper",
"repository": "https://github.com/g2crowd/widget",
"author": "Michael Wheeler",
Expand All @@ -17,6 +17,7 @@
},
"private": false,
"scripts": {
"build": "pack build",
"test": "jest",
"watch": "jest --watchAll"
},
Expand Down
23 changes: 16 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,24 @@ class AlreadyRegisteredError extends Error {
}

const strategies = initiationStrategies({
nextTick(pluginFn, $$, options) {
return window.setTimeout(() => pluginFn.call($$, options), 0);
nextTick(pluginFn, $$, options, ready) {
return window.setTimeout(() => pluginFn.call($$, options, ready), 0);
},

immediate(pluginFn, $$, options) {
return pluginFn.call($$, options) || {};
immediate(pluginFn, $$, options, ready) {
return pluginFn.call($$, options, ready) || {};
},

hover(pluginFn, $$, options) {
return $$.one('mouseover', () => pluginFn.call($$, options));
hover(pluginFn, $$, options, ready) {
return $$.one('mouseover', () => pluginFn.call($$, options, ready));
},
});

function emit($el, eventName) {
const event = new Event(eventName);
$el.get(0).dispatchEvent(event);
}

const widget = function({ attr, data }, loadEvents, fragmentLoadEvents) {
const selector = selectorBuilder({ attr, data });
const registered = {};
Expand All @@ -95,6 +100,10 @@ const widget = function({ attr, data }, loadEvents, fragmentLoadEvents) {
return;
}

const ready = () => {
emit($$, 'vvidget:initialized')
};

if (!existingPlugin) {
const pluginName = camelize(name);
const options = $.extend(
Expand All @@ -103,7 +112,7 @@ const widget = function({ attr, data }, loadEvents, fragmentLoadEvents) {
extractOptions(data, pluginName)
);

strategies.get(pluginFn.init)(pluginFn, $$, options);
strategies.get(pluginFn.init)(pluginFn, $$, options, ready);

$$.data(`vvidget:${name}`, true);
}
Expand Down

0 comments on commit f98b72c

Please sign in to comment.