Skip to content

Commit

Permalink
fix DataModel.clone() (#184)
Browse files Browse the repository at this point in the history
* fix DataModel.clone()

* 2.6.71
  • Loading branch information
kbarbounakis authored Jan 5, 2025
1 parent a7200d9 commit 4fe462f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
10 changes: 6 additions & 4 deletions data-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// noinspection ES6ConvertVarToLetConst

var _ = require('lodash');
var {cloneDeep} = require('lodash');
var {sprintf} = require('sprintf-js');
var Symbol = require('symbol');
var path = require('path');
Expand Down Expand Up @@ -578,10 +579,11 @@ DataModel.prototype.initialize = function() {
* @returns {DataModel} Returns a new DataModel instance
*/
DataModel.prototype.clone = function(context) {
var result = new DataModel(this);
if (context)
result.context = context;
return result;
// create new instance
var cloned = new DataModel(cloneDeep(this)).silent(this.isSilent());
// set context or this model context
cloned.context = context || this.context;
return cloned;
};
/**
* @this DataModel
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@themost/data",
"version": "2.6.70",
"version": "2.6.71",
"description": "MOST Web Framework Codename Blueshift - Data module",
"main": "index.js",
"types": "index.d.ts",
Expand Down
12 changes: 12 additions & 0 deletions spec/DataModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,16 @@ describe('DataModel', () => {
expect(model.caching).toBe('none');
});

it('should clone model', () => {
const model = context.model('Employee').silent();
expect(model).toBeTruthy();
const cloned = model.clone();
expect(cloned instanceof DataModel);
expect(cloned.name).toEqual(model.name);
// change something to parent
model.caching = 'always';
expect(cloned.caching).not.toBe(model.caching);
expect(cloned.isSilent()).toBeTruthy();
});

});
Binary file modified spec/test2/db/local.db
Binary file not shown.

0 comments on commit 4fe462f

Please sign in to comment.