Skip to content

Commit fc1c073

Browse files
committed
Merge pull request #47 from wmde/noCloning2
Never clone Group objects
2 parents fcb909a + 285bd29 commit fc1c073

File tree

2 files changed

+10
-19
lines changed

2 files changed

+10
-19
lines changed

src/Group.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ var SELF = wb.datamodel.Group = function WbDataModelGroup(
2929
if( !$.isFunction( GroupableCollectionConstructor ) ) {
3030
throw new Error( 'Item container constructor needs to be a Function' );
3131
}
32-
if( !( ( new GroupableCollectionConstructor() ) instanceof wb.datamodel.GroupableCollection ) ) {
33-
throw new Error( 'Item container constructor needs to implement GroupableCollection' );
34-
}
3532
if( !$.isFunction(
3633
GroupableCollectionConstructor.prototype[groupableCollectionGetKeysFunctionName]
3734
) ) {
@@ -40,7 +37,6 @@ var SELF = wb.datamodel.Group = function WbDataModelGroup(
4037
}
4138

4239
this._key = key;
43-
this._GroupableCollectionConstructor = GroupableCollectionConstructor;
4440
this._groupableCollectionGetKeysFunctionName = groupableCollectionGetKeysFunctionName;
4541
this.setItemContainer( groupableCollection || new GroupableCollectionConstructor() );
4642
};
@@ -52,12 +48,6 @@ $.extend( SELF.prototype, {
5248
*/
5349
_key: null,
5450

55-
/**
56-
* @property {Function}
57-
* @private
58-
*/
59-
_GroupableCollectionConstructor: null,
60-
6151
/**
6252
* @property {string}
6353
* @private
@@ -81,8 +71,7 @@ $.extend( SELF.prototype, {
8171
* @return {wikibase.datamodel.GroupableCollection}
8272
*/
8373
getItemContainer: function() {
84-
// Do not allow altering the encapsulated container.
85-
return new this._GroupableCollectionConstructor( this._groupableCollection.toArray() );
74+
return this._groupableCollection;
8675
},
8776

8877
/**
@@ -92,6 +81,10 @@ $.extend( SELF.prototype, {
9281
* match the key registered with the Group instance.
9382
*/
9483
setItemContainer: function( groupableCollection ) {
84+
if( !( groupableCollection instanceof wb.datamodel.GroupableCollection ) ) {
85+
throw new Error( 'groupableCollection must be a GroupableCollection' );
86+
}
87+
9588
var keys = this._getItemContainerKeys( groupableCollection );
9689

9790
for( var i = 0; i < keys.length; i++ ) {
@@ -101,10 +94,7 @@ $.extend( SELF.prototype, {
10194
}
10295
}
10396

104-
// Clone the container to prevent manipulation of the items using the original container.
105-
this._groupableCollection = new this._GroupableCollectionConstructor(
106-
groupableCollection.toArray()
107-
);
97+
this._groupableCollection = groupableCollection;
10898
},
10999

110100
/**

tests/Group.tests.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,10 @@ QUnit.test( 'setItemContainer() & getItemContainer()', function( assert ) {
176176
group = createGroup( 'key', container ),
177177
newContainer = getTestContainer( 'key', 3 );
178178

179-
assert.ok(
180-
group.getItemContainer() !== container,
181-
'Not returning original container.'
179+
assert.strictEqual(
180+
container,
181+
group.getItemContainer(),
182+
'getItemContainer() does not clone.'
182183
);
183184

184185
assert.ok(

0 commit comments

Comments
 (0)