wildpeaks / 2019-package-dom-entry-context

[ARCHIVED] Reads JSON-encoded data from an arbitrary HTMLElement data-context attribute.

Home Page:https://www.npmjs.com/package/@wildpeaks/dom-entry-context

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

DOM Entry Context

Github Release

Typescript function to read JSON-encoded data from an HTMLElement attribute.

Install:

npm install @wildpeaks/dom-entry-context

Example: Read a number from the default attribute data-context

index.html:

<div id="mycontainer" data-context="123">

script.ts:

import {Context, getContext} from '@wildpeaks/dom-entry-context';

const context: Context = getContext('mycontainer');
console.log(context.element); // Reference to #mycontainer
console.log(context.data);    // 123

Example: Read an object from a custom attribute

index.html:

<div id="mycontainer" data-myprop='{"hello": {"world": 123}}'>

script.ts:

import {Context, getContext} from '@wildpeaks/dom-entry-context';

const context: Context = getContext('mycontainer', 'data-myprop');
console.log(context.element);          // Reference to #mycontainer
console.log(context.data);             // {"hello": {"world": 123}}
console.log(context.data.hello);       // {"world": 123}
console.log(context.data.hello.world); // 123

About

[ARCHIVED] Reads JSON-encoded data from an arbitrary HTMLElement data-context attribute.

https://www.npmjs.com/package/@wildpeaks/dom-entry-context

License:MIT License


Languages

Language:TypeScript 100.0%