TodoMVC with ember, id 不递增

TodoMVC with ember, id does not increment

我正在遵循 emberjs 的入门指南,现在可以添加待办事项了。我的问题是,当我添加一个待办事项时,它的 id 值为 null - 有没有一种实用的方法可以自动增加它?

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

});

当您调用 this.store.createRecord() 时,您有一个 "option" 可以自动生成一个 id(参见 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-数据默认使用RestAdapter(参见here),所以需要添加客户端生成id的方法...