Skip to content

Commit

Permalink
Implemented clone for App and View.
Browse files Browse the repository at this point in the history
'Fixed' clone implementation for Model (now returns a Model with all
protoypes properties not only all App.Properties).

Model constructor now clones 'prototype' and 'dataItem' otherwise
obervables in nested models won't be cloned.

Fixes #93
  • Loading branch information
Joscha Rohmann committed May 25, 2016
1 parent 4adedb5 commit a3099ed
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/mvc/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ define([
this._super([_this, prototype, dataItem, collection]);
};

prototype = prototype || {};
prototype = blocks.clone(prototype, true) || {};
prototype.options = prototype.options || {};

return blocks.inherit(Model, ExtendedModel, prototype);
Expand Down Expand Up @@ -475,6 +475,10 @@ define([
this.View.Defaults = blocks.observable({
options: { }
}).extend();
},
// Application is a singleton. So return a reference instead of a clone.
clone: function () {
return this;
}
};
});
18 changes: 13 additions & 5 deletions src/mvc/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ define([
*/
function Model(application, prototype, dataItem, collection) {
var _this = this;
// deep clone dataItem, otherwise we don't clone obervables nested in Models
dataItem = blocks.clone(dataItem, true);
this._application = application;
this._prototype = prototype;
this._collection = collection;
this._initialDataItem = blocks.clone(dataItem, true);

Expand Down Expand Up @@ -271,17 +274,22 @@ define([
},

clone: function () {
return new this.constructor(blocks.clone(this._initialDataItem, true));
var self = this;
var data = {};
blocks.each(this._prototype, function (val, key) {
data[key] = blocks.clone(self[key], true);
});
return new this.constructor(data);
},

_setPropertyValue: function (property, propertyValue) {
var propertyName = property.propertyName;
if (blocks.isFunction(this[propertyName])) {
if (property.isObservable) {
this[propertyName] = this._createObservable(property, propertyValue);
} else if (blocks.isFunction(this[propertyName])) {
this[propertyName](propertyValue);
this._dataSource.update(this.dataItem());
} else if (property.isObservable) {
this[propertyName] = this._createObservable(property, propertyValue);
} else {
} else {
this[propertyName] = function () {
return propertyValue;
};
Expand Down
4 changes: 4 additions & 0 deletions src/mvc/View.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ define([

_error: function () {
this.loading(false);
},
// View is a singleton so return a reference
clone: function () {
return this;
}
};

Expand Down

0 comments on commit a3099ed

Please sign in to comment.