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(); //=> {}
相關文章
相關標籤/搜索