2014-09-25 11:32:08 -04:00
|
|
|
(function (global) {
|
|
|
|
"use strict";
|
|
|
|
|
2021-06-01 10:49:03 -04:00
|
|
|
function aliasMethod(methodName) {
|
|
|
|
return function() {
|
|
|
|
return this[methodName].apply(this, arguments);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2014-09-25 11:32:08 -04:00
|
|
|
function empty(obj) {
|
|
|
|
var key;
|
|
|
|
for (key in obj) if (obj.hasOwnProperty(key)) return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var Ember = global.Ember,
|
2021-06-01 10:49:03 -04:00
|
|
|
get = Ember.get, set = Ember.set,
|
|
|
|
isArray = Ember.isArray, getProperties = Ember.getProperties,
|
|
|
|
notifyPropertyChange = Ember.notifyPropertyChange,
|
|
|
|
meta = Ember.meta, defineProperty = Ember.defineProperty;
|
2014-09-25 11:32:08 -04:00
|
|
|
|
2021-06-01 10:49:03 -04:00
|
|
|
var hasOwnProp = Object.prototype.hasOwnProperty;
|
2014-09-25 11:32:08 -04:00
|
|
|
|
2021-06-01 10:49:03 -04:00
|
|
|
var BufferedProxyMixin = Ember.Mixin.create({
|
|
|
|
buffer: null,
|
2014-09-25 11:32:08 -04:00
|
|
|
hasBufferedChanges: false,
|
|
|
|
|
2021-06-01 10:49:03 -04:00
|
|
|
hasChanges: Ember.computed.readOnly('hasBufferedChanges'),
|
|
|
|
|
|
|
|
applyChanges: function() {
|
|
|
|
return this.applyBufferedChanges(...arguments);
|
2014-09-25 11:32:08 -04:00
|
|
|
},
|
|
|
|
|
2021-06-01 10:49:03 -04:00
|
|
|
discardChanges: function() {
|
|
|
|
return this.discardBufferedChanges(...arguments);
|
|
|
|
},
|
2014-09-25 11:32:08 -04:00
|
|
|
|
2021-06-01 10:49:03 -04:00
|
|
|
init: function() {
|
|
|
|
this.initializeBuffer();
|
|
|
|
set(this, 'hasBufferedChanges', false);
|
|
|
|
this._super(...arguments);
|
|
|
|
},
|
|
|
|
|
|
|
|
initializeBuffer: function(onlyTheseKeys) {
|
|
|
|
if(isArray(onlyTheseKeys) && !empty(onlyTheseKeys)) {
|
|
|
|
onlyTheseKeys.forEach((key) => delete this.buffer[key]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
set(this, 'buffer', Object.create(null));
|
|
|
|
}
|
|
|
|
},
|
2014-09-25 11:32:08 -04:00
|
|
|
|
2021-06-01 10:49:03 -04:00
|
|
|
unknownProperty: function(key) {
|
|
|
|
var buffer = get(this, 'buffer');
|
|
|
|
return (hasOwnProp.call(buffer, key)) ? buffer[key] : this._super(key);
|
|
|
|
},
|
|
|
|
|
|
|
|
setUnknownProperty: function(key, value) {
|
|
|
|
var m = meta(this);
|
|
|
|
|
|
|
|
if (m.proto === this || (m.isInitializing && m.isInitializing())) {
|
|
|
|
defineProperty(this, key, null, value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
var props = getProperties(this, ['buffer', 'content']),
|
|
|
|
buffer = props.buffer,
|
|
|
|
content = props.content,
|
|
|
|
current,
|
|
|
|
previous;
|
|
|
|
|
|
|
|
if (content != null) {
|
|
|
|
current = get(content, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
previous = hasOwnProp.call(buffer, key) ? buffer[key] : current;
|
|
|
|
|
|
|
|
if (previous === value) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-25 11:32:08 -04:00
|
|
|
|
|
|
|
if (current === value) {
|
|
|
|
delete buffer[key];
|
|
|
|
if (empty(buffer)) {
|
2021-06-01 10:49:03 -04:00
|
|
|
set(this, 'hasBufferedChanges', false);
|
2014-09-25 11:32:08 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
buffer[key] = value;
|
2021-06-01 10:49:03 -04:00
|
|
|
set(this, 'hasBufferedChanges', true);
|
2014-09-25 11:32:08 -04:00
|
|
|
}
|
|
|
|
|
2021-06-01 10:49:03 -04:00
|
|
|
notifyPropertyChange(content, key);
|
|
|
|
|
2014-09-25 11:32:08 -04:00
|
|
|
return value;
|
|
|
|
},
|
|
|
|
|
2021-06-01 10:49:03 -04:00
|
|
|
applyBufferedChanges: function(onlyTheseKeys) {
|
|
|
|
var props = getProperties(this, ['buffer', 'content']),
|
|
|
|
buffer = props.buffer,
|
|
|
|
content = props.content,
|
2014-09-25 11:32:08 -04:00
|
|
|
key;
|
2021-06-01 10:49:03 -04:00
|
|
|
|
|
|
|
Object.keys(buffer).forEach((key) => {
|
|
|
|
if (isArray(onlyTheseKeys) && onlyTheseKeys.indexOf(key) === -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-25 11:32:08 -04:00
|
|
|
set(content, key, buffer[key]);
|
2021-06-01 10:49:03 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
this.initializeBuffer(onlyTheseKeys);
|
|
|
|
|
|
|
|
if (empty(get(this, 'buffer'))) {
|
|
|
|
set(this, 'hasBufferedChanges', false);
|
2014-09-25 11:32:08 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-06-01 10:49:03 -04:00
|
|
|
discardBufferedChanges: function(onlyTheseKeys) {
|
|
|
|
var props = getProperties(this, ['buffer', 'content']),
|
|
|
|
buffer = props.buffer,
|
|
|
|
content = props.content,
|
2014-09-25 11:32:08 -04:00
|
|
|
key;
|
|
|
|
|
2021-06-01 10:49:03 -04:00
|
|
|
this.initializeBuffer(onlyTheseKeys);
|
|
|
|
|
|
|
|
Object.keys(buffer).forEach((key) => {
|
|
|
|
if (isArray(onlyTheseKeys) && onlyTheseKeys.indexOf(key) === -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyPropertyChange(content, key);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (empty(get(this, 'buffer'))) {
|
|
|
|
set(this, 'hasBufferedChanges', false);
|
2014-09-25 11:32:08 -04:00
|
|
|
}
|
2021-06-01 10:49:03 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
hasChanged: function(key) {
|
|
|
|
var props = getProperties(this, ['buffer', 'content']),
|
|
|
|
buffer = props.buffer,
|
|
|
|
content = props.content,
|
|
|
|
key;
|
|
|
|
|
|
|
|
if (typeof key !== 'string' || typeof get(buffer, key) === 'undefined') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get(buffer, key) !== get(content, key)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-09-25 11:32:08 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-01 10:49:03 -04:00
|
|
|
var BufferedProxy = Ember.ObjectProxy.extend(BufferedProxyMixin);
|
|
|
|
|
2014-09-25 11:32:08 -04:00
|
|
|
// CommonJS module
|
|
|
|
if (typeof module !== 'undefined' && module.exports) {
|
|
|
|
module.exports = BufferedProxy;
|
|
|
|
} else if (typeof define === "function" && define.amd) {
|
2021-06-01 10:49:03 -04:00
|
|
|
define("ember-buffered-proxy/proxy", function (require, exports, module) {
|
2014-09-25 11:32:08 -04:00
|
|
|
return BufferedProxy;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
global.BufferedProxy = BufferedProxy;
|
|
|
|
}
|
|
|
|
}(this));
|