Skip to content
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

can.Observe.Clone #435

Closed
wants to merge 3 commits into from
Closed

can.Observe.Clone #435

wants to merge 3 commits into from

Conversation

airhadoken
Copy link
Contributor

Cloning an Observe instance, by calling clone() on it, copies its properties (optionally deep-cloning nested Observes) to a new object that's an instance of the class's Clone child class. The original Observe's prototype is available in the clone object's prototype chain, in addition to the merge() function, which pushes any changes back to the original object and effectively destroys the clone copy.

Additionally, saving a Clone of a can.Model functions much like a merge() with a round-trip to the AJAX endpoint.

Example use:
make a clone of a page model to populate an Edit form. The original display of the model data is unchanged while pushing form data into the clone copy. When the form is saved by calling save() on the clone model, the original model and any of its live-bound views are updated.

@justinbmeyer
Copy link
Contributor

@airhadoken Are you still interested in seeing this land? I've got some time to look at new 2.1 features.

@param deep true to make Clones of all sub-observables.
*/
Observe.prototype.clone = function(deep) {
return new this.constructor.Clone({original : this, deep : !!deep});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's likely possible to only define the Clone "class" on demand instead of every time a can.Observe "class" is defined.

Could this check if this.constructor.Clone exists and create it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible, but it will generate a TypeError if someone attempts to do an instanceof check against the nonexistent Clone class if the check happens before the first cloning creates the class.

@bmomberger-reciprocity
Copy link
Contributor

I still think there's value in it, especially since reciprocity/ggrc-core still has that use case for it. I was interested in hearing whether anyone else would find it to be of value, though #311 shows that at very least the deep copy would be useful. If we can come to a consensus on the line items @justinbmeyer raised, I'll put priority on fixing/tidying it up for release.

@daffl daffl added this to the 2.3.0 milestone Feb 18, 2015
@daffl daffl removed this from the 2.3.0 milestone Oct 22, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants