7.7 Models -- Working with Records

Modifying Attributes数组

1. 一旦一条record被加载,你能够开始改变它的属性。在Ember.js对象中属性的行为就像正常的属性。做出改变就像设置你想要改变的属性同样简单:app

var tyrion = this.store.findRecord('person', 1);
// ...after the record has loaded
tyrion.set('firstName', "Yollo");

2. 对于修改属性来讲,全部的Ember.js的方便性都是可用的。例如,你可使用Ember.ObjectincrementProperty辅助器:函数

person.incrementProperty('age'); // Happy birthday!

3. 经过检查它的isDirty属性,你能够告诉一条发生显著改变的record是否被保存了。经过使用changedAttributes函数,你也能够发现record的哪一部分发生了变化而且原始值是什么。changedAttributes返回一个对象,它的keys是发生变化的属性而且值是一个values数组[oldValue, newValue]this

person.get('isAdmin');      //=> false
person.get('isDirty');      //=> false
person.set('isAdmin', true);
person.get('isDirty');      //=> true
person.changedAttributes(); //=> { isAdmin: [false, true] }

4. 在这一点上,你能够经过save()持久化你的变化或者你能够回滚你的改变。调用rollback()还原全部changedAttributes到原始值。spa

person.get('isDirty');      //=> true
person.changedAttributes(); //=> { isAdmin: [false, true] }

person.rollback();

person.get('isDirty');      //=> false
person.get('isAdmin');      //=> false
person.changedAttributes(); //=> {}