Skip to content

Commit a3099ed

Browse files
author
Joscha Rohmann
committed
Implemented clone for App and View.
'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
1 parent 4adedb5 commit a3099ed

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/mvc/Application.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ define([
134134
this._super([_this, prototype, dataItem, collection]);
135135
};
136136

137-
prototype = prototype || {};
137+
prototype = blocks.clone(prototype, true) || {};
138138
prototype.options = prototype.options || {};
139139

140140
return blocks.inherit(Model, ExtendedModel, prototype);
@@ -475,6 +475,10 @@ define([
475475
this.View.Defaults = blocks.observable({
476476
options: { }
477477
}).extend();
478+
},
479+
// Application is a singleton. So return a reference instead of a clone.
480+
clone: function () {
481+
return this;
478482
}
479483
};
480484
});

src/mvc/Model.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ define([
1010
*/
1111
function Model(application, prototype, dataItem, collection) {
1212
var _this = this;
13+
// deep clone dataItem, otherwise we don't clone obervables nested in Models
14+
dataItem = blocks.clone(dataItem, true);
1315
this._application = application;
16+
this._prototype = prototype;
1417
this._collection = collection;
1518
this._initialDataItem = blocks.clone(dataItem, true);
1619

@@ -271,17 +274,22 @@ define([
271274
},
272275

273276
clone: function () {
274-
return new this.constructor(blocks.clone(this._initialDataItem, true));
277+
var self = this;
278+
var data = {};
279+
blocks.each(this._prototype, function (val, key) {
280+
data[key] = blocks.clone(self[key], true);
281+
});
282+
return new this.constructor(data);
275283
},
276284

277285
_setPropertyValue: function (property, propertyValue) {
278286
var propertyName = property.propertyName;
279-
if (blocks.isFunction(this[propertyName])) {
287+
if (property.isObservable) {
288+
this[propertyName] = this._createObservable(property, propertyValue);
289+
} else if (blocks.isFunction(this[propertyName])) {
280290
this[propertyName](propertyValue);
281291
this._dataSource.update(this.dataItem());
282-
} else if (property.isObservable) {
283-
this[propertyName] = this._createObservable(property, propertyValue);
284-
} else {
292+
} else {
285293
this[propertyName] = function () {
286294
return propertyValue;
287295
};

src/mvc/View.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ define([
224224

225225
_error: function () {
226226
this.loading(false);
227+
},
228+
// View is a singleton so return a reference
229+
clone: function () {
230+
return this;
227231
}
228232
};
229233

0 commit comments

Comments
 (0)