samedi 2 mai 2015

Ember.js: Save a record in the local store from DS.RESTAdapter

I'm very new to Ember and the front end world and I've been working on this problem for very long.

I have an adapter that retrieves records from a JSON API:

    App.PostAdapter = DS.RESTAdapter.extend({
        find: function() {
            var url = 'I MAKE AN AJAX CALL TO THIS URL WITH JSONP AND IT RETURNS JSON CONTAINING MANY OBJECTS I WANTED TO SAVE IN THE STORE'; 
            return $.getJSON(url).then(function(data) {
                return data.map(function(result) {
                    var newPost = store.createRecord('post', {
                        title:   result.title,
                        content: result.content
                    });
                    newPost.save();
                })
            })
        }
    })

I have tried many ways but I simply could not get the store reference there (I already set up the post model). Also, I'm not even sure if it is the correct/recommended way to do this (Fetch the json from API and save it as soon as it is retrieved). I think it could work if I do this in the PostRoute but does it break the convention?

Sorry I know it is a total newbie question.




Aucun commentaire:

Enregistrer un commentaire