简体   繁体   中英

Ember-data with ajax call on a different server

I'm quite new with Ember and I wanted to use Ember-Data as mush as I could but I'm fetching data from an other server.

So far, I didn't use ember-data for anything unless for the user login information... I wanted to know if it was smart to fetch the information from the server and to save it in Ember-Data to use it straight after.

Server data ==> Ember-Data ==> Display information on the app

And when I wanted to modify some information this goes like that :

Information updated ==> Ember-Data ==> Server

Is this could be a good way to use ember-data with an ajax call ? Or is there is any other proper method to do it ?

I must say that for every call I've got a lot of data back, and I don't know if I should create every field in my model or if ember-data is doing it itself.

Thanks for your help.

[edit] After I reading your answer I tried to figure it out how to do it but its hard..

For example I have to send a GET request to my server like this :

store.find('enquiries'); This should return me all of my enquiries on my server.

But how the store build for it ?

Should I do just this in my store.js ?

App.store = DS.Store.extend({
    adapter: '-active-model'
})

App.Enquiries = DS.Model.extend({

});

In my Model I've put this :

DS.ActiveModelAdapter.reopen({
    host: 'http://localhost/'
});

I've tried to read the doc, but there is not much explanation about it :/

Option 1: You can use ActiveModel

 DS.ActiveModelAdapter.reopen({
      host: 'http://api.your-api/v1'
    });

and in your store

  App.Store = DS.Store.extend({
    adapter: '-active-model'
  });

Option 2: Rest Adapter

DS.RESTAdapter.reopen({
  host: 'http://api.your-api/v1'
});

That's it, now all your request are going to be redirected to your new "host"

Reference http://emberjs.com/guides/models/the-rest-adapter/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM