Skip to content

Instantly share code, notes, and snippets.

@mhwinkler
Last active March 19, 2019 21:05
Show Gist options
  • Select an option

  • Save mhwinkler/0eb0e6eb3d79577f71c6226b27190d27 to your computer and use it in GitHub Desktop.

Select an option

Save mhwinkler/0eb0e6eb3d79577f71c6226b27190d27 to your computer and use it in GitHub Desktop.
/***************************
* SYNOPSIS:
* Widgets that have already appeared on a page can't be recreated on the same page,
* and it seems to be because of this code.
***************************/
/***************************
* EXISTING CODE SNIPPET A:
***************************/
b.loadWidgets = function(a) {
var c, d;
return d = b.$(".ct-widget"),
b.console.log("Found", d.length, "widget tag(s):", d),
c = function(a, c) {
return b.Widget.replaceTagWithWidget(a).then(function() {
/***********************
* BOND NOTE:
* the line above errors, because `then` can't be called off of `undefined`,
* which is the return value of `replaceTagWithWidget` when called on a widget
* that had previously been created
***********************/
return c()
})
}
,
"synchronous" === a ? (b.console.log("Loading widgets in series..."),
async.eachSeries(d, c)) : (b.console.log("Loading widgets in parallel..."),
async.each(d, c)),
!0
}
/***************************
* EXISTING CODE SNIPPET B:
***************************/
replaceTagWithWidget: function(a) {
var c, d, e, f, g, h, i, j;
return g = {},
window.location.search && (e = window.location.search.substring(1).split("&"),
e.forEach(function(a) {
var b;
return b = a.split("="),
g[b[0]] = b[1]
})),
j = a.getAttribute("data-src"),
g.__locale__ && (d = "__locale__=" + g.__locale__,
f = j.indexOf("?") >= 0 ? "&" : "?",
j += f + d),
c = parseInt(a.getAttribute("data-widget-id"), 10),
h = j,
/***********************
* BOND NOTE:
* that's partially because the ternary below: `this.getWidgetByWidgetId(c)` is the
* reference to the already created widget
***********************/
null != this.getWidgetByWidgetId(c) ? (b.console.group("Error"),
b.console.log("Widget tags with widget id", c, "already exist!"),
b.console.log("Will not convert the following widget tag:", a),
/***********************
* BOND NOTE:
* and partially because this returns undefined when the widget already exists
***********************/
void b.console.groupEnd()) : (i = this.addWidget({
id: c,
src: h
}),
b.$(a).replaceWith(i.el),
i.onRender)
/***********************
* BOND NOTE:
* when it could safely respond with this
***********************/
},
/***************************
* BOND SUGGESTED CHANGES
* FOR CODE SNIPPET B:
***************************/
replaceTagWithWidget: function(a) {
var c, d, e, f, g, h, i, j;
return g = {},
window.location.search && (e = window.location.search.substring(1).split("&"),
e.forEach(function(a) {
var b;
return b = a.split("="),
g[b[0]] = b[1]
})),
j = a.getAttribute("data-src"),
g.__locale__ && (d = "__locale__=" + g.__locale__,
f = j.indexOf("?") >= 0 ? "&" : "?",
j += f + d),
c = parseInt(a.getAttribute("data-widget-id"), 10),
h = j,
/***********************
* BOND NOTE:
* change `this.getWidgetByWidgetId(c)` to keep the widget if it already exists and supply it
* so that the ternary below decides "should we create a new widget or do we have one?"
***********************/
null != (i = this.getWidgetByWidgetId(c)) ? (b.console.group("Error"),
b.console.log("Widget tags with widget id", c, "already exist!"),
b.console.log("Will not convert the following widget tag:", a),
b.console.groupEnd()) : (i = this.addWidget({
id: c,
src: h
})),
/***********************
* BOND NOTE:
* then `.replaceWith` is safe regardless of whether the widget had already been created
***********************/
b.$(a).replaceWith(i.el),
/***********************
* BOND NOTE:
* and return this promise (which will immediately resolve in the case of widgets who have been previously created)
***********************/
i.onRender
},
/***************************
* BOND SUGGESTED CHANGES
* FOR `sendMessage()`:
***************************/
c.prototype.sendMessage = function(a) {
/***********************
* BOND NOTE:
* adding this try/catch for when the widget is no longer accessible
***********************/
try{return "string" != typeof a && (a = JSON.stringify(a)),
this.el.contentWindow.postMessage(a, "*")}catch(e){}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment