How do I set up Ember Data to use the JSONP datatype when making its ajax calls? I am going to be using Ember with Phonegap and need to make cross-domain requests.
            Asked
            
        
        
            Active
            
        
            Viewed 2,001 times
        
    2 Answers
7
            
            
        It's much easier to override the private ajaxOptions function instead of using jQuery. Ember's pipeline includes the removal of the jQuery dependency anyways. So do this instead:
adapters/application.js:
import DS from 'ember-data';
export default DS.RESTAdapter.extend({
    ajaxOptions: function(url, type, options) {
        var hash = this._super(url, type, options);
        hash.dataType = "jsonp";
        return hash;
    }
});
It would be create if the Ember core team could expose a public method to officially support this (instead of hacking a private api).
 
    
    
        Jamie Chong
        
- 832
- 9
- 13
- 
                    This is definitely the better answer – anushr Jun 24 '15 at 00:33
4
            You need to create your own adapter which uses jsonp, you can do just that by extending a current one, have a look.
App.MyAdapter= DS.RESTAdapter.extend({})
Then you need to implement the find method among others, to use jsonp, could be something like this
App.MyAdapter= DS.RESTAdapter.extend({
  find: function(store, type, id) {
     var item;
     $.ajax({
      url: 'http://api.domain/someModel',
      dataType: 'jsonp',
      success: function(response){
        item = App.someModel.create(order))
      }
    });
    return item;
  },
This is not tested, but it should give you the idea of how i can be done. :)
 
    
    
        MartinElvar
        
- 5,695
- 6
- 39
- 56
- 
                    This worked, since thankfully there is only one method in Ember-Data that requires defining the dataType. – edborden Feb 11 '14 at 01:37
- 
                    1One other thing to note however, which I figured out while doing this, is that Phonegap doesn't require using JSONP, just setting domain accessibility in the config.xml. – edborden Feb 11 '14 at 01:38
