with Ember Data, how do you call commit?

北城以北 提交于 2020-01-14 02:51:33

问题


I am trying to build a simple Todo List using all the last routes and data stuff for ember. You can find my full repo here

I have my store set up like so:

EmberTodo.Store = DS.Store.extend({
  revision: 11,
  adapter: DS.RESTAdapter.create({bulkCommit: false})
});

The line of code that is giving me trouble comes from here:

EmberTodo.CreateItemView = Ember.TextField.extend({
  insertNewline: function() {
    EmberTodo.Item.createRecord({description: this.get('value')});
    this.set("value", "");
  }
});

From what I understand, calling createRecord doesn't create the record, but instead I need to call commit() somewhere. However, I cannot figure out where. Anyone have any ideas?


回答1:


From what I understand, calling createRecord doesn't create the record, but instead I need to call commit() somewhere. However, I cannot figure out where. Anyone have any ideas?

Sure. To get this working with the smallest possible

EmberTodo.CreateItemView = Ember.TextField.extend({
  insertNewline: function() {
    item = EmberTodo.Item.createRecord({description: this.get('value')});
    item.get('transaction').commit();
    this.set("value", "");
  }
});

I've placed a simplified, working example using DS.FixtureAdapter here: http://jsbin.com/ugipap/1/edit

Done, right?

Kinda. Thing is, you really don't want to be doing this kinda thing from within a view. Consider refactoring to move this logic into the controller layer, or possibly the router depending on the situation.



来源:https://stackoverflow.com/questions/14489852/with-ember-data-how-do-you-call-commit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!