Accessing an object in an iframe

70 views
Skip to first unread message

DAZ

unread,
Aug 11, 2012, 2:14:09 PM8/11/12
to sina...@googlegroups.com
Hi,

I have a Post object that I want to access on a page that also contains an iframe. I also need to access the same post object from within the iframe. Does anybody know if there's any way of doing this without having to access the database again to find the post objec (like in the code below)t?

get '/:id' do
  @post = Post.get(params[:id])
  slim :show
end

get '/iframe/:id' do
  @post = Post.get(params[:id])
  slim :iframe
end

@@show
h1== @post.title
iframe src="/iframe/#{@post.id}"

@@iframe
== @post.body

cheers,

DAZ
  


Jason Rogers

unread,
Aug 11, 2012, 8:23:05 PM8/11/12
to sina...@googlegroups.com
I suppose you could serialize it as JSON in the session.

before do
  @post = session[:post] ? Post.new(JSON(session[:post])) : Post.get(params[:id])
end

after do
  session[:post] = JSON(@post)
end

get '/:id' do
  slim :show
end

get '/iframe/:id' do
  slim :show
end
--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sinatrarb/-/xVNy0-TDBjAJ.
To post to this group, send email to sina...@googlegroups.com.
To unsubscribe from this group, send email to sinatrarb+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sinatrarb?hl=en.


--

--
Jason Rogers

DAZ

unread,
Aug 12, 2012, 3:52:17 PM8/12/12
to sina...@googlegroups.com
Hi Jason,

Thanks for the reply. That's the only other idea I had of doing it (but I hadn't thought of how to code it, so thanks :)

I guess using JSON would be good if I start updating the iframe with Ajax later. I was hoping there might be another way of avoiding hitting the database twice without having to use sessions and/or JSON, but I guess that the iframe has to be loaded as a separate request.

Thanks again, cheers,

DAZ

Alper Akgün

unread,
Aug 12, 2012, 4:10:03 PM8/12/12
to sina...@googlegroups.com
i suggest
- you consider communicating from parent to iframe; in js, with JSON which is easier if you are the same domain
- or reconsider hitting database twice; not a big deal.
- or reconsider using iframes.

--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sinatrarb/-/zwMCk6RvHtcJ.

Carlos Eduardo L. Lopes

unread,
Aug 13, 2012, 9:56:01 AM8/13/12
to sina...@googlegroups.com
I agree with Alper's suggestions, and mainly the third: reconsider using iframes.. but, if they are really necessary, you can use memcache to store the response body.. so, you will store the response body in the memcache when you do the first request, and retrieve the body from memcache when you do the second request (iframe)
--
Carlos Eduardo L. Lopes
Skype: carloslopespollares | Twitter: @_carloslopes


Jason Rogers

unread,
Aug 13, 2012, 10:31:13 AM8/13/12
to sina...@googlegroups.com
I think that introducing Memcache for something like this is overkill. If Memcache will be leveraged in some other way, that's another story.
--
Carlos Eduardo L. Lopes
Skype: carloslopespollares | Twitter: @_carloslopes


--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To post to this group, send email to sina...@googlegroups.com.
To unsubscribe from this group, send email to sinatrarb+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sinatrarb?hl=en.


--

--
Jason Rogers

Carlos Eduardo L. Lopes

unread,
Aug 13, 2012, 10:39:28 AM8/13/12
to sina...@googlegroups.com
I agree with you too, but, store the response body inside a session like Jason said is overkiil too, because it will consumes a lot of traffic, decrease performance and other issues that we are now.. memcache is just an option that works, but it's  true that it's too much for a too simple thing..

Jason Rogers

unread,
Aug 13, 2012, 2:04:36 PM8/13/12
to sina...@googlegroups.com
Interesting.

How does using the session consume a lot of traffic? You're going to make the same amount of HTTP requests whether you use a session, memcache, or hit the database every time. (You save yourself only one request if you communicate between the iframe and parent window via JavaScript.) If the size of the body is what you're concerned about, again it'll be the same if you go back to the server despite what implementation you use on the backend.

Don't optimize prematurely, optimization requires profiling.

--
Jason Rogers

Carlos Eduardo L. Lopes

unread,
Aug 13, 2012, 2:23:51 PM8/13/12
to sina...@googlegroups.com
Yes, if you make only one request (parent), store the body inside a session, and fill the iframe with the body that is already in the session, i agree that this is the best option..

but i don't know if this works because i rarely work with iframes :/

Carlos Eduardo L. Lopes

unread,
Aug 13, 2012, 2:26:28 PM8/13/12
to sina...@googlegroups.com
And i was concerning about traffic because if do you have a large body, make requests with a session that has a big size isn't good (imho, and in my humble experience :) )

Jason Rogers

unread,
Aug 13, 2012, 3:30:54 PM8/13/12
to sina...@googlegroups.com
True. 2 requests for a large body can be time- and bandwidth-consuming. The question becomes "what's too large?", which is where profiling is key.

--
Jason Rogers

Carlos Eduardo L. Lopes

unread,
Aug 13, 2012, 3:40:30 PM8/13/12
to sina...@googlegroups.com
Sure! :)
Reply all
Reply to author
Forward
0 new messages