简体   繁体   English

使用right.js用flash [:notice]的内容刷新div?

[英]Refreshing a div with the contents of flash[:notice] using right.js?

I am using inline haml templates in my sinatra app. 我在我的sinatra应用程序中使用内联haml模板。 I have a @@layout with a section like this: 我有一个@@layout其中包含以下部分:

#message
  - if flash[:notice]
    %section.notice= flash[:notice]
  - if flash[:error]
    %section.error= flash[:error]

When I use flash[:notice] = "Hello!" 当我使用flash[:notice] =“你好!” in a route & click on a link, the next page happily says Hello" in the #message div. great. 在路线中点击一个链接,下一页愉快地在#message div中说“你好”。很棒。

So here is the problem, I am using right.js to add some ajax niceness to my app, The behavior of rack-flash is inconsistent at best. 所以这就是问题所在,我正在使用right.js为我的应用程序添加一些ajax的好处,机架式闪存的行为充其量是不一致的。

Most of the time, you click a link ( .linkey ), a little bit of javascript intercepts it & loads into a #content div (this part works as well), then the '#message' div is reloaded & the flash for the previous action is displayed... the next time you click a link... about 80% of the time, the rest of the time nothing is displayed. 大多数情况下,你单击一个链接( .linkey ),一点点javascript拦截它并加载到#content div(这部分也可以),然后重新加载'#message'div和闪存显示上一个操作...下次单击链接时...大约80%的时间,其余时间不显示任何内容。

This is my js: 这是我的js:

"a.linkey".onClick(function(event) {
   event.stop();
   $('content').load( [ "/", this.get('id'), ].join("") );
   $('message').load( '/message' );
});

I would like is something like this: 我希望是这样的:

1) Click link 1)点击链接

2) the link target (/foo) is loaded into #content 2)链接目标(/ foo)被加载到#content

3) #message is reloaded with the message (from the route flash[:notice] = "bar") 3) #message重新加载消息(来自路由flash[:notice] =“bar”)

4) #content now displaying /foo & #message displays "bar" 4) #content现在显示/foo#message显示“bar”

I have also tried this with $('message').load( '/message' ); 我也尝试过$('message').load( '/message' ); but that either loads nothing into #message or fills #message with "/message" (a string not the contents). 但是要么没有加载到#message ,要么用“/ message”填充#message (字符串不是内容)。

I would like to know what is going on here? 我想知道这里发生了什么? Is it something with rack-flash or right.js? 是机架式闪存还是right.js? or something else? 或者是其他东西? I can provide more code if needed but there really isn't much more beyond the basic framework as I am just starting on this project. 如果需要的话,我可以提供更多的代码,但除了基本框架之外,我刚刚开始这个项目。

For AJAX requests running through a controller action, you'd need to use flash.now so that the flash message will be available for the current request. 对于通过控制器操作运行的AJAX请求,您需要使用flash.now以便闪存消息可用于当前请求。 So in your case, flash.now[:notice] = "bar" . 所以在你的情况下, flash.now[:notice] = "bar"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM