Created
August 8, 2012 12:52
-
-
Save pjlammertyn/3294879 to your computer and use it in GitHub Desktop.
Ember.Editable mixin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ember.Editable = Ember.Mixin.create({ | |
_originalPropertyStates: Ember.Map.create(), | |
_isEditing: false, | |
isDirty: null, | |
init: function () { | |
this._super(); | |
}, | |
willDestroy: function () { | |
var $this = this; | |
for (var prop in this.getOwnProperties()) { | |
Ember.removeBeforeObserver($this, prop, $this, '_beforePropertyChange'); | |
//Ember.removeObserver($this, prop, $this, '_propertyChanged'); | |
} | |
}, | |
startEditing: function () { | |
if (this.get('_isEditing')) { | |
return; | |
} | |
this.set('_isEditing', true); | |
this.set('_originalPropertyStates', Ember.Map.create()); | |
var $this = this; | |
var mixinProps = Ember.A(); | |
Ember.Mixin.mixins(this).forEach(function (mixin) { | |
if (mixin.toString().startsWith('Ember.')) { | |
mixin.keys().forEach(function (key) { | |
if (!mixinProps.contains(key)) { | |
mixinProps.pushObject(key); | |
} | |
}); | |
} | |
}); | |
for (var prop in this.getOwnProperties()) { | |
if (!mixinProps.contains(prop)) { //SKIP MIXIN PROPERTIES IN THE EMBER NAMESPACE | |
Ember.addBeforeObserver($this, prop, $this, '_beforePropertyChange'); | |
//Ember.addObserver($this, prop, $this, '_propertyChanged'); | |
} | |
} | |
}, | |
_beforePropertyChange: function (obj, propName) { | |
var origProps = this.get('_originalPropertyStates'); | |
if (!origProps.has(propName)) { | |
origProps.set(propName, Ember.get(obj, propName)); | |
this.notifyPropertyChange('isDirty'); | |
} | |
}, | |
cancelEditing: function () { | |
var $this = this; | |
this.get('_originalPropertyStates').forEach(function (key, value) { | |
$this.set(key, value); | |
}); | |
this.endEditing(); | |
}, | |
endEditing: function () { | |
this.set('_isEditing', false); | |
var $this = this; | |
for (var prop in this.getOwnProperties()) { | |
if (!Ember.Editable.keys().contains(prop)) { | |
Ember.removeBeforeObserver($this, prop, $this, '_beforePropertyChange'); | |
} | |
} | |
this.set('_originalPropertyStates', Ember.Map.create()); | |
this.notifyPropertyChange('isDirty'); | |
}, | |
dirtyProps: function () { | |
if (!this.get('_isEditing')) { | |
return Ember.A(); | |
} | |
return this.get('_originalPropertyStates.keys.list'); | |
} .property(), | |
isDirty: function () { | |
if (!this.get('_isEditing')) { | |
return false; | |
} | |
var changedPropKeys = this.get('_originalPropertyStates.keys'); | |
if (!Ember.empty(changedPropKeys)) { | |
return !changedPropKeys.isEmpty(); | |
} | |
return false; | |
} .property() | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://stackoverflow.com/questions/9211844/reflection-on-emberjs-objects-how-to-find-a-list-of-property-keys-without-knowi | |
//http://jsfiddle.net/algesten/5Hj9E/ | |
Ember.Object.prototype.getOwnProperties = function () { | |
var props = {}; | |
for (var prop in this) { | |
if (this.hasOwnProperty(prop) | |
&& prop.indexOf('__ember') < 0 | |
&& prop.indexOf('_super') < 0 | |
&& Ember.typeOf(this.get(prop)) !== 'function') { | |
props[prop] = this[prop]; | |
} | |
} | |
return props; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment