2

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();
    }
}

});

PrestaShopDeveloper
  • 3,110
  • 3
  • 21
  • 30
Julien Vincent
  • 1,210
  • 3
  • 17
  • 40

1 Answers1

3

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...

Kalman
  • 8,001
  • 1
  • 27
  • 45