Extending Reader and Proxy - An Ext Flickr Image Gallery
October 17, 2008 by Evan Trimboli
With the growing popularity of Web 2.0, more companies are providing public APIs for developers to create new and exciting compilations. These APIs can be used to grab small snippets of content which can be used in an application or site. Commonly referred to as a mashup, content from different sources are collected and presented to the user in a single interface. By aggregating the data in a single place, users remain on your site and need not be inconvenienced by multiple interfaces.
Flickr is a popular image sharing management tool. Amongst a number of services, it allows users to create online photo albums and the ability to categorize and tag photos. In this post we’ll look at retrieving data from Flickr by extending Ext’s native data package to display an image gallery. The image gallery widget will support the ability to search images based on how they are tagged inside Flickr.
Getting Started
To begin using the Flickr web services, we need to get an API key. The API key is sent with all requests and is used to identify the caller. Instructions on obtaining a key can be found here (there is no cost to get one). Once we have our key, we can look at the various methods the service exposes to us, which can be broadly classified as blog information, user information, contact information and photos. It is important to note that a lot of the methods in the Flickr API require authentication via a username and password. In our example, we will be using a public method only, which can be run without needing to be authenticated.
Cross domain restrictions and workarounds
Due to security restrictions in web browsers, Ajax requests can only be made to the domain of origin. Practically, this means we can’t make a call to another domain to request data. This poses a problem for building our image gallery. Enter Ext.data.ScriptTagProxy! ScriptTagProxy uses a technique called JSONP or JSON with padding that bypasses cross-domain restrictions by using script tags to transport data. It works by dynamically creating a new function in the window scope of the document. It then injects a script tag into the head of the document pointing at the remote resource with a url parameter of the name of the function it just created. The server-side must then use this url parameter to wrap around an object literal. Check out the Ext documentation for a Java Example.
Helper Classes
For the image gallery, I’ve written 2 small classes to assist with loading data from Flickr. The first is the FlickrProxy, which handles simple tasks like appending the api key and the method to each request. The second class is the FlickrPhotoReader, which defines the response format of photos. There are a number of different objects that get returned, so additional readers could be created for that purpose. The other class created is a FlickrWindow class. It is used to do basic paging operations and handle searching for images by tags.
Putting It All Together
Now we have all our components, we can use our new gallery. The code to create it is as follows:
var store = new Ext.data.Store({ reader: new Flickr.FlickrPhotoReader(), proxy: new Flickr.FlickrProxy({ apiKey: 'd4396e08e2a00f2c913d1fe5aa040c16', method: 'photos.search' }) }); var win = new Flickr.FlickrWindow({ store: store, width: 400, height: 400 }); win.show();
The example code has been packaged as a zip and is available here.
After populating our data store, we can now navigate between the images. Some images are available in multiple sizes. You can use the image menu drop down to select what size you’d like displayed. If an image is not available in the size you have chosen you will receive a white image back with the text “This photo is currently unavailable.”
Next Steps
This example only uses a very small portion of the Flickr API. You can use the FlickrProxy as a base for accessing other methods offered in the Flickr API. The methods which require user authentication could prove to be particularly interesting for those of you who use Flickr on a regular basis. We’re always amazed at the wealth of talent and creativity in the Ext community and would love to see what you can create withthis small stepping stone into Flickr’s data services.

Posted on October 17th, 2008 at 2:30 am
I am really impressed. Ext is truly music! The elegance demonstrated extending ext is genius.
Posted on October 17th, 2008 at 12:00 pm
This is something awesome enough, even we can keep this widget in our websites and even in our blogs too…. Excellent! I LOVED IT…..
Posted on October 17th, 2008 at 2:51 pm
[...] After getting your own API key then just do follow respective steps can be viewed at this URL - http://extjs.com/blog/2008/10/17/ext-flickr-image-gallery/ to built your own [...]
Posted on October 20th, 2008 at 9:17 pm
Excellent Job! I loved it!!
Just a question, how can we set a specific user’s photo album?
e.g http://www.flickr.com/photos/bpcsv to show bpcsv user photos only?
Posted on October 21st, 2008 at 3:12 am
This is nice as far as it goes, but why not add to the existing flickr/YAHOO images search browser instead of starting from scratch?
Sanjoy, the class linked to below allows you to search for images, then navigate on to the Flickr gallery from which the image was found:
http://extjs.com/forum/showthread.php?t=18653
Posted on October 21st, 2008 at 11:20 pm
good job ! i love it !
Posted on October 30th, 2008 at 3:01 pm
[...] need only some kind of JSON or XML interface. Google has one - ScriptTag. So after reading this post about creating custom Ext.data.JsonReader and Ext.data.ScriptTagProxy. /** Ext Proxy for Google [...]
Posted on November 13th, 2008 at 7:04 am
This is quite inforamtive…Thank you
Posted on November 18th, 2008 at 5:29 am
Любопытно, а есть хоть кто-то, кто не согласен с автором?
Posted on December 21st, 2008 at 7:01 pm
[...] Extending Reader and Proxy - An Ext Flickr Image Gallery ( javascript webdev Ext Flickr ) [...]
Posted on January 4th, 2009 at 6:49 pm
Buy amoxicillin….
Buy amoxicillin….