Skip to content

Commit

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

* 2.17.1
  • Loading branch information
kbarbounakis authored Jan 5, 2025
1 parent c634b76 commit 7ac50b5
Show file tree
Hide file tree
Showing 4 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
@@ -1,5 +1,6 @@
// MOST Web Framework 2.0 Codename Blueshift BSD-3-Clause license Copyright (c) 2017-2022, THEMOST LP All rights reserved
var _ = require('lodash');
var {cloneDeep} = require('lodash');
var {sprintf} = require('sprintf-js');
var Symbol = require('symbol');
var pluralize = require('pluralize');
Expand Down Expand Up @@ -580,10 +581,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.17.0",
"version": "2.17.1",
"description": "MOST Web Framework Codename Blueshift - Data module",
"main": "index.js",
"scripts": {
Expand Down
12 changes: 12 additions & 0 deletions spec/DataModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,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();
});

});

0 comments on commit 7ac50b5

Please sign in to comment.