问题
Has anyone had success working with a front-end only applications with Ember.js and consuming endpoints from an external host? If so please share your experience.
Here are a couple of options.
- Make the requests through a local proxy (just pass through).
- Use a proxy + Rails so that you can work the way Ember.js wants you to.
- Use CORS requests to get around the XSS issue.
It seems like the canonical approach to Ember.js is to use a local Rails app to serve up json to an Ember.js app. From what research my team has done, it doesn't seem like CORS is a commmon way to work with Ember.js.
EDIT:
Regarding the comment below, this is really more a question of how to use Ember-data with an external api rather than Ember.js itself.
回答1:
Server-client communication is not something that Ember.JS is solving for you, it uses jQuery for this, so there shouldn't be any kind of restriction on Ember.JS' side.
All the things you listed are legitimate ways of dealing with APIs that are served from different domains, depending on the user agent support you want to achieve (some clients don't support CORS at all, or in limited forms).
回答2:
**Approach 1**
If you look at this blog post: http://eng.netwallet.com/2012/04/17/simple-cross-domain-ajax-with-a-wormhole-5/
On the host page, they are using porthole.js and an Ember object that sets up the porthole windowProxy and listens for messages from it.
Porthole.js is a small Javascript library that makes it safe and easy to communicate with cross domain iFrames.
You can always use this approach from within ember-data and the RestAdapter to override things like gryzzly mentioned.
**Approach 2**
Also in the post below, they are doing cors using goliath as proxy to work with ember-data and elasticsearch but with their own custom adapter:
http://www.elasticsearch.org/tutorials/2012/08/22/javascript-web-applications-and-elasticsearch.html
The whole code from the post is here: https://gist.github.com/3369662
来源:https://stackoverflow.com/questions/12729771/how-do-you-use-ember-js-with-external-non-local-api-endpoints