Started playing with Ember.js and ember-data. I followed this tutorial which guides you to create a Twitter timeline show.
It’s pretty neat, but it doesn’t use ember-data, which it seems to me a good way to deal with REST APIs.
In my playground, I created something like this:
EmberTwitter.Tweet = DS.Model.extend({
avatar: null,
screen_name: null,
text: null,
date: null,
url: "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=naoj_gior"
});
var tweets = EmberTwitter.store.findAll(EmberTwitter.Tweet);
I would like to know if ember-data is prepared to work with external APIs (and how it deals with Same Origin Policy…) because it seems to me and after looking to the docs, that is not. I see the following error in the console:
Resource failed to load: file:///tweets
What would be a good way to fetch an external API with ember.js?
You’re correct– because it runs in your browser it’s subject to the sames rules as every other library/plain JS. You will have to use some sort of server proxy that passes your request through (a PHP script, etc).
You can see an example of this sort of proxy (written in PHP) here:
http://www.daniweb.com/web-development/php/code/216729/php-proxy-solution-for-cross-domain-ajax-scripting
The general idea is, you simply use your server to make the request on behalf of the Javascript and echo it out.
It also looks like someone used an IFrame to achieve the same purpose. The following example also is Ember.js, so it might work for you:
http://eng.netwallet.com/2012/04/17/simple-cross-domain-ajax-with-a-wormhole-5/