Rails Turbolinks破烂不堪 [英] Rails turbolinks breaking pnotify

查看:77
本文介绍了Rails Turbolinks破烂不堪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Rails应用程序中,它具有它,以便您在列表中投票,订阅或取消订阅时,它将使用pnotify显示弹出窗口.

In my rails application, I have it so that when you vote, subscribe or unsubscribe from a list, it will show a popup using pnotify.

这很好,直到用户导航到另一个页面,现在它不再起作用.仅在加载或重新加载页面时有效.

This works fine until the user navigates to a different page, and now it is no longer working. This only works when page is loaded or reloaded.

我知道这是因为Rails 4应用程序中的Turbolinks,但它似乎执行了除pnotify之外的所有其他JavaScript代码.

I know this is because turbolinks in a rails 4 application but it seems to execute all my other javascript code apart from the pnotify.

其他信息..

subscribe/unsubscribe和表决按钮具有方法post,并在远程执行,这意味着我有一个单独的文件名subscription.js.erb,unsubscribe.js.erb和vote.js.erb,用于处理javascript的执行用户单击按钮后输入代码.

The subscribe/unsubscribe and vote buttons have the method post and are executed remotely which means I have a seperate file names subscribe.js.erb, unsubscribe.js.erb and vote.js.erb that handle the execution of the javascript code when the user has clicked on the buttons.

这是我如何创建新的pnotify对象的示例:

this is an example of how I am creating a new pnotify object:

new PNotify({
    title:  "Subscribed!",
    text: "<%= @message %>",
    type: 'info',
    opacity: .8,
    animation: {
        effect_in: 'show',
        effect_out: 'slide'
    },
    buttons: {
        sticker: false
    }
}).get().click(function() {
            $(this).remove();
        });

我还使用一种宝石来进行化名,称为宝石'pnotify-rails'

I am also using a gem for pnotify called gem 'pnotify-rails'

推荐答案

我遇到了相同的问题,但是由于PNotify丢失了它的

I've had the same issues, but that happens because PNotify loses its context, as you can see in that line the plugin assign the context as $('body'), but when you navigate using turbolink, the body is replaced so the context is lost.

您需要做什么?

您需要在turbolink事件'page:load'中分配上下文

You need to assign the context in turbolink's event 'page:load'

$(document).on('page:load',function(){
	PNotify.prototype.options.stack  = {
		dir1: "down",
		dir2: "left",
		push: "bottom",
		spacing1: 25,
		spacing2: 25,
		context: $("body")
    }
        
});

这篇关于Rails Turbolinks破烂不堪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆