Search code examples
ember.jsember-dataember-clitodomvc

TodoMVC with ember, id does not increment


I am following the getting started guide from emberjs, and am at the point where I can add todos. My problem is though that when I add a todo it has an id value of null - is there a practical way to auto increment this?

var TodosController = Ember.ArrayController.extend({
actions: {
    createTodo: function() {
        var title = this.get('newTitle');
        if (!title.trim()) {
            return;
        }

        var todo = this.store.createRecord('todo', {
            title: title,
            isCompleted: false
        });

        this.set('newTitle', '');

        todo.save();
    }
}

});


Solution

  • When you call this.store.createRecord() you have an "option" to have an id autogenerated (see here) Ultimately though, that responsibility is delegated to an adapter. If your adapter has generateIdForRecord() method - this will be used to create an id. So, for example, FixtureAdapter implements this method as follows (see here):

    generateIdForRecord: function(store) {
      return "fixture-" + counter++;
    }
    

    ember-data uses RestAdapter by default (see here), so you would need to add the method for the id to be generated on the client...