简体   繁体   中英

Ember-data REST Api delete record

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?

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.

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