Skip to content

Instantly share code, notes, and snippets.

@elgordino
Last active July 31, 2017 07:46
Show Gist options
  • Save elgordino/7c5aa0f001f4a2e0633df359de2985f9 to your computer and use it in GitHub Desktop.
Save elgordino/7c5aa0f001f4a2e0633df359de2985f9 to your computer and use it in GitHub Desktop.
This gist is for emberfire users. It's a Mixin that adds a property to DS.Model attributes that causes their value to be set to the special 'ServerValue.TIMESTAMP' on save.
import Ember from 'ember';
import firebase from 'firebase';
const { Mixin, get, set } = Ember;
/**
* A small Mixin for DS.Model for emberfire users that will set attributes that have
* 'setToServerTimeStampOnSave' to the firebase 'ServerValue.TIMESTAMP' on save.
* eg:
* clientUpdatedTime: attr('number', { setToServerTimestampOnSave: true }),
*/
export default Mixin.create({
save() {
get(this.store.modelFor(get(this, 'constructor.modelName')), 'attributes').forEach((value, key) => {
if (value.options.setToServerTimestampOnSave && !get(this, 'isDeleted')) {
set(this, key, firebase.database.ServerValue.TIMESTAMP);
}
});
return this._super(...arguments);
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment