-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix x-id when used with morph (#3919)
* fix x-id when used with morph * fix $id caching mechanism * remove .only * fix tests
- Loading branch information
1 parent
3d75916
commit 0effdae
Showing
3 changed files
with
84 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
import { interceptClone } from "../clone" | ||
import { directive } from "../directives" | ||
import { setIdRoot } from '../ids' | ||
|
||
directive('id', (el, { expression }, { evaluate }) => { | ||
let names = evaluate(expression) | ||
|
||
names.forEach(name => setIdRoot(el, name)) | ||
}) | ||
|
||
interceptClone((from, to) => { | ||
// Transfer over existing ID registrations from | ||
// the existing dom tree over to the new one | ||
// so that there aren't ID mismatches... | ||
if (from._x_ids) { | ||
to._x_ids = from._x_ids | ||
} | ||
}) | ||
|
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 |
---|---|---|
@@ -1,14 +1,46 @@ | ||
import { magic } from '../magics' | ||
import { closestIdRoot, findAndIncrementId } from '../ids' | ||
import { interceptClone } from '../clone' | ||
|
||
magic('id', el => (name, key = null) => { | ||
let root = closestIdRoot(el, name) | ||
magic('id', (el, { cleanup }) => (name, key = null) => { | ||
let cacheKey = `${name}${key ? `-${key}` : ''}` | ||
|
||
let id = root | ||
? root._x_ids[name] | ||
: findAndIncrementId(name) | ||
return cacheIdByNameOnElement(el, cacheKey, cleanup, () => { | ||
let root = closestIdRoot(el, name) | ||
|
||
return key | ||
? `${name}-${id}-${key}` | ||
: `${name}-${id}` | ||
let id = root | ||
? root._x_ids[name] | ||
: findAndIncrementId(name) | ||
|
||
return key | ||
? `${name}-${id}-${key}` | ||
: `${name}-${id}` | ||
}) | ||
}) | ||
|
||
interceptClone((from, to) => { | ||
// Transfer over existing ID registrations from | ||
// the existing dom tree over to the new one | ||
// so that there aren't ID mismatches... | ||
if (from._x_id) { | ||
to._x_id = from._x_id | ||
} | ||
}) | ||
|
||
function cacheIdByNameOnElement(el, cacheKey, cleanup, callback) | ||
{ | ||
if (! el._x_id) el._x_id = {} | ||
|
||
// We only want $id to run once per an element's lifecycle... | ||
if (el._x_id[cacheKey]) return el._x_id[cacheKey] | ||
|
||
let output = callback() | ||
|
||
el._x_id[cacheKey] = output | ||
|
||
cleanup(() => { | ||
delete el._x_id[cacheKey] | ||
}) | ||
|
||
return output | ||
} |
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