0
votes

I have a REST API consumed by an ember app. Here is the .hbs which lists a model. I can give new elements with the "save" action, and every item has also a "delete" action.

    {{ input value=name }}
    {{ input value=value }} 
            <button {{ action "save" this }}>Save</button>
    <table class="table">
    {{#each item in model}}
        <tr><td>{{item.name}}</td><td>{{float2 item.value}}</td><td><button {{ action "delete" item}}>delete</button></td></tr>
    {{/each}}
    </table>

So far works everything fine. The problem if i insert (save) a new element, i cant "delete" it, it has id:null, and the request to the api has no id at the end of the URL. (although on the client side the item will be removed from the list)

here is the controller's actions:

actions:  {
    save: function (record) {

        var vat = this.store.createRecord('vat',{
            name: this.get('name'),
            value: parseFloat(this.get('value'))
        });

        vat.save();
    },
    delete: function(record){
        console.log(record);
        record.deleteRecord();
        record.save();;
    }
}

My guess is after the insert the API doesnt have the correct response, and ember-data doesnt know the new item's id. (maybe im wrong) what respons (JSON structure) expects the RESTAdapter, with which status code?

1
I have a similar issue, my JSON is correct and I get this problem only the first time a new item is saved... every following objects are okayPascal Boutin

1 Answers

1
votes

In your case the response from server should have 200 or 201 status code and the body should look like this

{
    "vat": {
        "id": 1,
        "name": "Name",
        "value": 10.5
    }
}

You can also override normalize function in RESTAdapter to adjust the format of server response.