-
-
Save toolmantim/358667 to your computer and use it in GitHub Desktop.
| # In answer to: http://twitter.com/jimwhimpey/status/11727440383 | |
| # | |
| # "I want to output 10 Flickr photos one at a time as the REST response | |
| # is returned for each rather than wait for all 10." | |
| class FlickrStreamer | |
| def initialize(photos) | |
| @photos = photos | |
| end | |
| def each | |
| @photos.each do |photo| | |
| yield fetch_photo_from_flickr(photo) | |
| end | |
| end | |
| def fetch_photo_from_flickr(photo) | |
| # Call the flickr API, return the response | |
| end | |
| end | |
| get '/photos' do | |
| halt FlickrStreamer.new(["photo1", "photo2", "photo3"]) | |
| end |
Basically I'm asking, is it possible to output stuff to a template before everything has finished executing?
No, it's really how the stack is designed (especially with layouts, etc).
Why the hell are you wanting to do that though? Maybe there's a better way you should be approaching it...
I wanted to do it because I'm getting more than just basic info about each photo so it takes 2 extra REST calls for each photo which for 10 photos results in 21 individual REST calls before you see even one photo. It'd be nice if there was a Flickr method which returned way more data, then it'd only require one call and be nice and fast.
I guess what I could do is only grab 2 or 3 photos originally and then make individual JS calls for the rest.
JS calls is totally what you want to do. You get to control the experience then too, and if you had lots of little sinatra instances working away you'd end up getting all that stuff loading in quicker than just one-at-a-time.
Yeah, looks like JS calls to my own Sinatra back end will be the way to go. Loving this stuff so far. Thanks for your help!
nice! You could also call Flickr directly in the browser via json-p. Just an idea.
So where does the output happen? In fetch_photo_from_flickr?
def fetch_photo_from_flickr(photo)
haml :index
end