0
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-01-18 10:52:19 +01:00

data: reword storage key in dom

This commit is contained in:
Johann-S 2020-06-17 10:45:07 +02:00
parent 5ad43fec83
commit 088c727a31
2 changed files with 15 additions and 15 deletions

View File

@ -16,22 +16,22 @@ const mapData = (() => {
let id = 1 let id = 1
return { return {
set(element, key, data) { set(element, key, data) {
if (typeof element.key === 'undefined') { if (typeof element.bsKey === 'undefined') {
element.key = { element.bsKey = {
key, key,
id id
} }
id++ id++
} }
storeData[element.key.id] = data storeData[element.bsKey.id] = data
}, },
get(element, key) { get(element, key) {
if (!element || typeof element.key === 'undefined') { if (!element || typeof element.bsKey === 'undefined') {
return null return null
} }
const keyProperties = element.key const keyProperties = element.bsKey
if (keyProperties.key === key) { if (keyProperties.key === key) {
return storeData[keyProperties.id] return storeData[keyProperties.id]
} }
@ -39,14 +39,14 @@ const mapData = (() => {
return null return null
}, },
delete(element, key) { delete(element, key) {
if (typeof element.key === 'undefined') { if (typeof element.bsKey === 'undefined') {
return return
} }
const keyProperties = element.key const keyProperties = element.bsKey
if (keyProperties.key === key) { if (keyProperties.key === key) {
delete storeData[keyProperties.id] delete storeData[keyProperties.id]
delete element.key delete element.bsKey
} }
} }
} }

View File

@ -15,7 +15,7 @@ describe('Data', () => {
}) })
describe('setData', () => { describe('setData', () => {
it('should set data in an element by adding a key attribute', () => { it('should set data in an element by adding a bsKey attribute', () => {
fixtureEl.innerHTML = '<div></div>' fixtureEl.innerHTML = '<div></div>'
const div = fixtureEl.querySelector('div') const div = fixtureEl.querySelector('div')
@ -24,7 +24,7 @@ describe('Data', () => {
} }
Data.setData(div, 'test', data) Data.setData(div, 'test', data)
expect(div.key).toBeDefined() expect(div.bsKey).toBeDefined()
}) })
it('should change data if something is already stored', () => { it('should change data if something is already stored', () => {
@ -40,7 +40,7 @@ describe('Data', () => {
data.test = 'bsData2' data.test = 'bsData2'
Data.setData(div, 'test', data) Data.setData(div, 'test', data)
expect(div.key).toBeDefined() expect(div.bsKey).toBeDefined()
}) })
}) })
@ -104,11 +104,11 @@ describe('Data', () => {
Data.setData(div, 'test', data) Data.setData(div, 'test', data)
expect(div.key).toBeDefined() expect(div.bsKey).toBeDefined()
Data.removeData(div, 'test2') Data.removeData(div, 'test2')
expect(div.key).toBeDefined() expect(div.bsKey).toBeDefined()
}) })
it('should remove data if something is stored', () => { it('should remove data if something is stored', () => {
@ -121,11 +121,11 @@ describe('Data', () => {
Data.setData(div, 'test', data) Data.setData(div, 'test', data)
expect(div.key).toBeDefined() expect(div.bsKey).toBeDefined()
Data.removeData(div, 'test') Data.removeData(div, 'test')
expect(div.key).toBeUndefined() expect(div.bsKey).toBeUndefined()
}) })
}) })
}) })