Last active
May 5, 2016 20:47
-
-
Save lukemelia/5632776 to your computer and use it in GitHub Desktop.
Buffered Proxy, extracted from Yapp codebase
This file contains 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
var empty, get, set, | |
__hasProp = {}.hasOwnProperty; | |
get = Ember.get; | |
set = Ember.set; | |
empty = function(obj) { | |
var key; | |
for (key in obj) { | |
if (!__hasProp.call(obj, key)) continue; | |
return false; | |
} | |
return true; | |
}; | |
Yapp.BufferedProxyMixin = Ember.Mixin.create({ | |
buffer: null, | |
hasBufferedChanges: false, | |
unknownProperty: function(key) { | |
var buffer; | |
buffer = this.buffer; | |
if (!(buffer != null ? buffer.hasOwnProperty(key) : void 0)) { | |
return this._super(key); | |
} | |
return buffer[key]; | |
}, | |
setUnknownProperty: function(key, value) { | |
var buffer, content, current, previous, _ref; | |
buffer = (_ref = this.buffer) != null ? _ref : this.buffer = {}; | |
content = this.get("content"); | |
if (content != null) { | |
current = get(content, key); | |
} | |
previous = buffer.hasOwnProperty(key) ? buffer[key] : current; | |
if (previous === value) { | |
return; | |
} | |
this.propertyWillChange(key); | |
if (current === value) { | |
delete buffer[key]; | |
if (empty(buffer)) { | |
this.set("hasBufferedChanges", false); | |
} | |
} else { | |
buffer[key] = value; | |
this.set("hasBufferedChanges", true); | |
} | |
this.propertyDidChange(key); | |
return value; | |
}, | |
applyBufferedChanges: function() { | |
var buffer, content, key; | |
buffer = this.buffer; | |
content = this.get("content"); | |
for (key in buffer) { | |
if (!__hasProp.call(buffer, key)) continue; | |
set(content, key, buffer[key]); | |
} | |
this.buffer = {}; | |
this.set("hasBufferedChanges", false); | |
return this; | |
}, | |
discardBufferedChanges: function() { | |
var buffer, content, key; | |
buffer = this.buffer; | |
content = this.get("content"); | |
for (key in buffer) { | |
if (!__hasProp.call(buffer, key)) continue; | |
this.propertyWillChange(key); | |
delete buffer[key]; | |
this.propertyDidChange(key); | |
} | |
this.set("hasBufferedChanges", false); | |
return this; | |
} | |
}); | |
Yapp.BufferedProxy = Em.ObjectProxy.extend(Yapp.BufferedProxyMixin); |
We use this regularly. It is near to what we use in production.
but we should likely extract it as a standalone repo.
This is really cool. There's one caveat (related more to ember-data of course) I reported in an issue. How do you guys work around this (if at all)?
actions:
save: ->
content = @get('content')
@applyBufferedChanges()
content.save().then ->
// sweet
, ->
// uh oh - now the record has our changes applied but they aren't valid
// we could rollback those changes, but the record will have gone from
// 'My Title' to 'My New Title' and back to 'My Title' - which would be odd for the user
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@lukemelia, do you guys use this code regularly? Is this the latest version of it?