with Ember Data, how do you call commit?

匿名 (未验证) 提交于 2019-12-03 09:14:57

问题:

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.



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