7

So on the GitHub documentation for Ratchet 2.0.2 I found the following statement.

Script tags containing JavaScript will not be executed on pages that are loaded with push.js. If you would like to attach event handlers to elements on other pages, document-level event delegation is a common solution.

Can someone please spell out exactly how to get a custom <script> to execute after being loaded by Push.js?

On my first page, I have a Table view, with several links to other pages, one of them being a link to a second page with a Twitter Feed widget on it.

<li class="table-view-cell media">
    <a class="navigate-right" href="Twitter.php" data-transition="slide-in">
        <span class="media-object pull-left icon icon-person"></span>
        <div class="media-body">
            Twitter Feed
        </div>
    </a>
</li>

The second page only contains the twitter feed widget code. When I browse to this page directly (without being loaded by Push.js) everything loads correctly, but when it is loaded via Push.js, the script is not executed.

Can someone please explain what I need to do to get this script to execute after being loaded by Push.js? I've searched Google, Stack Exchange, and Github\Ratchet issues and have not been able to find a good example of how to accomplish this.

One solution would be to add data-ignore="push" to the link, but I want to know how to do with WITH push.js.

<div class="content">
    <a class="twitter-timeline" href="https://twitter.com/XXXX" data-widget-id="XXXX">Tweets by XXX</a>
</div>
<script>
    !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+"://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
</script>
4

2 回答 2

7

编辑:下面是我最初解决这个问题的方法,效果很好,但我想出了一个更好的解决方案,我将其发布为这个问题的答案。


我终于弄明白了。

在您的第一页上,您需要执行以下操作...

var checkPage = function(){
    //Only run if twitter-widget exists on page
    if(document.getElementById('twitter-widget')) {
        loadTwitterFeed(document,"script","twitter-wjs");
    }
};

window.addEventListener('push', checkPage);

checkPage()每次通过推送加载新页面时都会执行。

于 2014-04-21T19:00:59.210 回答
1

刚刚对 Ratchet.js 进行了更改,以使每个页面的单个 js 工作更加优雅。(https://github.com/mazong1123/ratchet-pro

通过使用新的 ratchetPro.js,我们可以做到以下几点:

(function () {
    var rachetPageManager = new window.RATCHET.Class.PageManager();
    rachetPageManager.ready(function () {
       // Put your logic here.
    });
})();
于 2015-10-13T09:30:09.883 回答