-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from udduttaatlinkedIn/feature/adding-proxy-o…
…bject-util Adding ability to use proxy objects in order to avoid object mutations to original content object
- Loading branch information
Showing
5 changed files
with
108 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { isNone } from '@ember/utils'; | ||
import { guidFor } from '@ember/object/internals'; | ||
|
||
/** | ||
* On drag action we need to provide a property `isDraggingObject` to the UI. | ||
* This utility is used to create a wrapper object around the object passed to the proxy function. | ||
* We use this wrapper to prevent the `draggable-object` from mutating the original object by appending | ||
* `isDraggingObject` to the content. | ||
* | ||
* This unexpected mutation causes problems when the targeted content is not prepared to handle | ||
* the additional property, and potentially leaks local state onto an object that likely holds state | ||
* for the route or application more generally. | ||
*/ | ||
|
||
/** | ||
* @access public | ||
* Returns the passed object wrapped within new object. | ||
* Used to proxy draggable objects. | ||
* @param objectToProxy Object to proxy. | ||
* @returns {Object} Proxy object. | ||
*/ | ||
export function wrapper(objectToProxy) { | ||
// If we do not have any content for the object to proxy, | ||
// we do not wish to render that item. | ||
if (!isNone(objectToProxy)) { | ||
const guidKey = guidFor(objectToProxy); | ||
return { | ||
[guidKey]: objectToProxy, | ||
unwrappingKey: guidKey, | ||
id: objectToProxy.id | ||
}; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* @access public | ||
* Returns the content of the passed object. | ||
* Used to un-proxy draggable objects. | ||
* @param objectToUnproxy Object to un-proxy. | ||
* @returns {Object} Content of the proxy object. | ||
*/ | ||
export function unwrapper(objectToUnproxy) { | ||
if (!isNone(objectToUnproxy)) { | ||
return objectToUnproxy[objectToUnproxy.unwrappingKey]; | ||
} | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupTest } from 'ember-qunit'; | ||
import { | ||
wrapper, | ||
unwrapper, | ||
} from 'ember-drag-drop/utils/proxy-unproxy-objects'; | ||
import { guidFor } from '@ember/object/internals'; | ||
|
||
module( | ||
'Unit | Util | ember-drag-drop/utils/proxy-unproxy-objects', | ||
function(hooks) { | ||
setupTest(hooks); | ||
|
||
hooks.beforeEach(function() { | ||
this.testObject = { | ||
value: true, | ||
id: 123, | ||
}; | ||
this.testObjectGuid = guidFor(this.testObject); | ||
}); | ||
|
||
test('wrapper returns a new object containing content and ID feilds', function testProxyObjAction(assert) { | ||
assert.expect(2); | ||
assert.equal( | ||
wrapper(this.testObject)[this.testObjectGuid], | ||
this.testObject, | ||
'Wrapped object contains corresponding guid field containing the original object content' | ||
); | ||
|
||
assert.equal( | ||
wrapper(this.testObject).id, | ||
this.testObject.id, | ||
'Object contains ID field' | ||
); | ||
}); | ||
|
||
test('unwrapper returns back the original object', function testUnproxyObjAction(assert) { | ||
assert.expect(1); | ||
assert.deepEqual( | ||
unwrapper(wrapper(this.testObject)), | ||
this.testObject, | ||
'Returned object is same as the original test object' | ||
); | ||
}); | ||
} | ||
); |