javascriptcanjscanjs-model

CanJS Model findAll returns list of duplicate items


I'm using can.Model to retrieve data by id:

Invoice = can.Model({
    findAll: 'GET /invoices',
    create  : "POST /invoices",
    update  : "PUT /invoices/{id}",
    destroy : "DELETE /invoices/{id}"
},{});

When navigating to /invoices, the result is as expected, like:

[
    0: {
         "ID": "1",
         "Client": "Client1",
    },
    1: {
         "ID": "2",
         "Client": "Client2"
    }
]

However, the data retrieved with Invoice.findAll and logged to the console, it looks like this, with the same data item repeated for each element in the list:

[
    0: {
         "ID": "1",
         "Client": "Client1"
    },
    1: {
         "ID": "1",
         "Client": "Client1"
    }
]

The response from the server is correct, so why is it being interpreted as a list of identical items?


Solution

  • When data has an id field different from id, it should be specified using the id field. For instance, as mentioned in the documentation for can.Model in .NET it is common to use Id. In this case, the id field must be defined as ID:

    Invoice = can.Model({
        id: 'ID',
        findAll: 'GET /invoices',
        create  : "POST /invoices",
        update  : "PUT /invoices/{id}",
        destroy : "DELETE /invoices/{id}"
    },{});