-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial implementation of the Snapshot API #2623
Conversation
@private | ||
*/ | ||
var Snapshot = function(record) { | ||
var attributes = Object.create(null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ember.create
to get the polyfil
super cool! |
e7bf3b9
to
4e26221
Compare
Ah if it's an ordered set forEach is perfect |
I think there is a problem with the way of relationships are built. Since they are lazily cached, there may be some inconsistencies if for example a snapshot is created, its relationships are accessed, and in the meantime the record's relationships are modified. Unless I'm wrong could lead to wrong snapshot's relationships values. |
@@ -936,6 +937,10 @@ var Model = Ember.Object.extend(Ember.Evented, { | |||
this._notifyProperties(dirtyKeys); | |||
}, | |||
|
|||
snapshot: function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Underscore this, add doc @Private
783db24
to
6a8b76a
Compare
if (relationship && relationship.relationshipMeta.kind === 'hasMany') { | ||
return this.hasMany(keyName); | ||
} | ||
return this.attr(keyName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check if keyName
is attr, if not, pass to this.record.get()
snapshot.hasMany('foo', { ids: true }) |
bb2eea4
to
f4957ce
Compare
|
||
var get = Ember.get; | ||
|
||
var hasPropertyAccessors = Ember.__loader.require('ember-metal/platform')['hasPropertyAccessors']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is exported by Ember as Ember.platform.hasPropertyAccessors
since at least 1.8.
https://github.com/emberjs/ember.js/blob/v1.8.1/packages/ember-metal/lib/main.js#L183-L186
7e89904
to
c293f30
Compare
@knownasilya As @wecc described, the high order bit for me is that a snapshot represents the frozen state of a record at a particular moment in time. As we push everything towards being all-async all the time, you want to get a way to examine the current state of that record in the moment without triggering all sorts of side-effects, like loading relationships. |
@tomdale @wecc From what I understand from the code, I think there are still inconsistencies in the implementation. Since the relationships are lazy and the attributes are eager, actually when you create a snapshot, and the access its data, I think problems can occur if the relationships of the underlying are changed before the snapshot's ones are accessed. Though I just may be completely wrong. |
@sly7-7 You're absolutely correct. We discussed this at length and decided that you need to pre-emptively access the snapshot's relationships if you want to freeze them at that moment in time. If we eagerly snapshot all relationships as well as attributes, we may have to create thousands of snapshots that the user never needs. Definitely open to other suggestions here, because it's going to trip people up. But I don't know how to avoid making it easy to snapshot the entire object graph otherwise. |
I'm not sure to completely understand this feature. Can it be used to create "forked models" (à la EPF), so hat modifications are applied locally, and then saved independently (to avoid the very famous binding problem where properties are updated visually while the model is not saved yet) |
@bakura10 It is not that complex. It simply, at snapshot time, loops over all of the attributes of a model and copies their current values over into an immutable hash, giving you a "snapshot in time" of the state of that model. |
If this does what I think it will do, it's going to make me very very happy :) |
@method id | ||
@return {String} The ID of the snapshot's record | ||
*/ | ||
id: function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm nervous because now if you do record.id
it will return the id of the record. This could break serializers that are relying on this behavior.
Rebased, merge conflicts fixed. |
``` | ||
|
||
@method id | ||
@return {String} The ID of the snapshot's record |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@property id
@type {String}
Initial implementation of the Snapshot API
This is a first stab at the new Snapshot API,
serializer.serialize()
now receives a Snapshot instead of a record instance.To only useDone in this PR.attr()
,belongsTo()
andhasMany()
would require some work in serializers. Should that be done in the scope of this PR or separately?[@sly7-7]: Just to clean issues when that awesome work is merged:
Closes #2551, Fixes #2385, Fixes #2508, Closes #1491
Fixes #2750, Closes #2739