I use the Ratchet.js / push.js library to create a user interface for a mobile web application. In this library, links are processed by "clicking" the downloaded file into the ".content" DOM element, rather than loading the entire page. However, push.js does not load the scripts that it finds when the page loads, which disables my Knockout.js code.
I found a solution here on StackOverflow that works very well - just add an event listener for the push event. I changed it so that it can load any script on several pages and therefore it works with external script files:
window.addEventListener('push', function () { var scriptsList = document.querySelectorAll('script.js-custom');
On the target HTML page of the script, class and identifier tags are set, so they work with the above:
<script src="Challenge.js" class="js-custom" id="challenge.js"></script>
Note also that Knockout bindings must occur with a specific DOM element name so that knockout is not confused:
ko.cleanNode($("#ChallengePage")[0]); ko.applyBindings(challengeFn, $("#ChallengePage")[0]);
We use cleanNode to avoid "already related" errors.
OK! So all of this works great, and I hope that anyone struggling with this will find this useful.
HOWEVER, when a link gets a transition:
<a href="challenge.html" data-transition="slide-in">....
It then breaks down into “Unable to read the“ nodeType ”property from undefined. I thought that maybe it was just a problem of waiting for the transition to complete, but even if I replaced the script evaluation with:
scriptContents = script; setTimeout(function () { eval(scriptContents); }, 1000);
It does not help.
Any advice or help would be greatly appreciated! I don’t need to “click” the pages if I don’t use the transitions, so I hope that someone will have the last key to make it all work!
UPDATE: an error occurred because the call to "document.querySelectorAll" when using a transition uses the current document, not the one that was clicked. Also, using "webkitTransitionEnd" as my event also works, but it does not fix the document problem. That way, I can do this work, but for just one transition - now I have no way to load a document. Ideally, a solution that works if the links use the transition or not is what I'm looking for.