Search code examples
ember.jsember-data

Auto-Generate id in ember-data with Local Storage Adapter


i need a way to generating id for new record that added to store . I am using local storage adapter. and searching for some way and find functions like in local storage adapter :

generateIdForRecord: function () {
    return Math.random().toString(32).slice(2).substr(0, 5);
}

but the the problem with this function is this function create ids in string , I am need auto increment id in integer scope. like 1 or 2 or 2000 and if new record added the id is maximum of ids in record plus 1. I need doing this in client side not server.


Solution

  • If you want a ‍‍‍counter-type id, you can always do what fixture_adapter is doing, which is to have a variable called counter and increment it every time (see here).

    But then, what happens if a record gets deleted? Do you want to have another new record that has an id of an old deleted record? And if instead you have some sort of a global counter that ALWAYS increments by 1, which would account for the issue I am mentioning - then you are anyways just assigning pretty much random ids, so why not go with the approach of a generating random ids, which is where you started from? :)