Ember-data fetch data from server to store


#1

Hi,

I wanted to use ember-data to fetch my data from my node server into my store. I’ve looked the documentation and the guide but I can’t find any answer there… Because I’m getting a lot of data back, I can’t really write by hand every field from every request I need to do into my model…

Is it possible for ember-data to do it automatically without doing this :

App.post = DS.Model.extend({
  title: DS.attr('string'),
  author: DS.attr('string'),
  body: DS.attr('string')
}); 

I need to fetch my data here ‘http://localhost/mdf/’ and I would like to do some request like this : store.find('login', data); (data is an object to send to my server).

can you help me to start it with me ? I will be able to do the rest but I can’t figure it out how to start…

Thanks a lot for your help


#2

I manage to make it run, but I have an other problem with this…

in my first post I say that I wanted to do a request such as store.find('login', data); and my data looks like this :

 var data = {
                domain: 'mydomain',
                username: 'user',
                password: 'puser'
}

My server is configured to receive a get at ‘login’ but ember is requesting ‘logins’ with a ‘s’ ? So none of my request can reach my server… I can’t really modify it because I didn’t develop it.

Is there is a way to go around those issues ?


#3

Since you’re passing a hash to store.find, it thinks you’re making a query, therefore, it uses the plural form of the resource. With the way you currently have it, you’ll have to override the findQuery method in the adapter to send the request to the right location.


#4

Oh well I see, and is there is a particular way to do it ? That could be awesome if you could guide me a little bit more…

And btw, I was wondering if the store can generate itself the model dynamically after a find request ?


#5

It sounds like you’re using the REST adapter, and to be honest, your data model seems really weird. It definitely doesn’t sound like it implements the JSON-API like the REST adapter expects. My best advice would be to write your own adapter by extending the Adapter class. Then you can interact with your server with jQuery or a straight XmlHttpRequest object. You’ll avoid a lot of the headaches of trying to modify the REST adapter class to do what you need. I don’t know if there’s a tutorial on how to write your own adapter, but there’s certainly examples out there. The REST adapter is probably the only example you’ll need.


#6

yes I’m using the REST adapter. To give you an other example of what my server is giving me back :

{
 enquiries: {
    0: {
        name: "John",
        email; "randomEmail",
        other: "blabla"
    }
    1: {
        name: "Bob",
        email; "randomEmail2",
        other: "blabla"
    }
}

This is most likely what I’m receiving and I thought that would be good for ember as a JSON format to use. And I thought too that I could use this.store.find(‘enquiry’); to fetch all my data from http://localhost/enquiry… Because I already create my app but I didn’t use the Ember-Data and so now I regret it so much because I’m stuck at a point that if I continue to dev this app like that I would not be able to do everything I wanted.

Should I still write my own adapter ? Or I can still hope overrride the findQuery ?

Thanks for your help :wink: