Ember.js: how to save a model

天大地大妈咪最大 提交于 2019-12-23 09:18:37

问题


From the ember docs its clear you should be able to save a dirty model

var m = App.MyModel.find(10) ;
...
m.set("firstName", "John") ;
m.get("isDirty") ; // --> true

Now, I don't know how to save, things like

m.save() ;
App.MyModel.save(m) ;
//etc

do not work. Any suggestions ?

CHeers


回答1:


EDIT: This is now out of date with Ember Data 1.0 beta and onwards, please refer to Bart's answer

If you are using Ember-Data, you need to call commit() on the model's transaction.

m.get('transaction').commit()

or if you want to save every dirty object in your app

m.get('store').commit()



回答2:


The accepted answer is no longer valid since the release of Ember Data 1.0 (beta at the time of writing). Saving is much easier and more intuitive with Ember Data (1.0).

var person = this.store.createRecord('person');
person.set('frist_name', 'John');
person.set('last_name', 'Doe');
person.save();

It is also good to know that a save call returns a promise, which is resolved when the server returns a response.

person.save().then(function() {
  // SUCCESS
}, function() {
  // FAILURE
});


来源:https://stackoverflow.com/questions/14455570/ember-js-how-to-save-a-model

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