Skip to content

Never clone Group objects #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions src/Group.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ var SELF = wb.datamodel.Group = function WbDataModelGroup(
if( !$.isFunction( GroupableCollectionConstructor ) ) {
throw new Error( 'Item container constructor needs to be a Function' );
}
if( !( ( new GroupableCollectionConstructor() ) instanceof wb.datamodel.GroupableCollection ) ) {
throw new Error( 'Item container constructor needs to implement GroupableCollection' );
}
if( !$.isFunction(
GroupableCollectionConstructor.prototype[groupableCollectionGetKeysFunctionName]
) ) {
Expand All @@ -40,7 +37,6 @@ var SELF = wb.datamodel.Group = function WbDataModelGroup(
}

this._key = key;
this._GroupableCollectionConstructor = GroupableCollectionConstructor;
this._groupableCollectionGetKeysFunctionName = groupableCollectionGetKeysFunctionName;
this.setItemContainer( groupableCollection || new GroupableCollectionConstructor() );
};
Expand All @@ -52,12 +48,6 @@ $.extend( SELF.prototype, {
*/
_key: null,

/**
* @property {Function}
* @private
*/
_GroupableCollectionConstructor: null,

/**
* @property {string}
* @private
Expand All @@ -81,8 +71,7 @@ $.extend( SELF.prototype, {
* @return {wikibase.datamodel.GroupableCollection}
*/
getItemContainer: function() {
// Do not allow altering the encapsulated container.
return new this._GroupableCollectionConstructor( this._groupableCollection.toArray() );
return this._groupableCollection;
},

/**
Expand All @@ -92,6 +81,10 @@ $.extend( SELF.prototype, {
* match the key registered with the Group instance.
*/
setItemContainer: function( groupableCollection ) {
if( !( groupableCollection instanceof wb.datamodel.GroupableCollection ) ) {
throw new Error( 'groupableCollection must be a GroupableCollection' );
}

var keys = this._getItemContainerKeys( groupableCollection );

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

// Clone the container to prevent manipulation of the items using the original container.
this._groupableCollection = new this._GroupableCollectionConstructor(
groupableCollection.toArray()
);
this._groupableCollection = groupableCollection;
},

/**
Expand Down
7 changes: 4 additions & 3 deletions tests/Group.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,10 @@ QUnit.test( 'setItemContainer() & getItemContainer()', function( assert ) {
group = createGroup( 'key', container ),
newContainer = getTestContainer( 'key', 3 );

assert.ok(
group.getItemContainer() !== container,
'Not returning original container.'
assert.strictEqual(
container,
group.getItemContainer(),
'getItemContainer() does not clone.'
);

assert.ok(
Expand Down