new MutationObserver(option)
target 으로 설정된 element 의 변화를 감지
Parameters:
| Name | Type | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
option |
Partial.<IMutationObserverExtOption> |
Properties
|
- Source:
- See:
Example
import { MutationObserver, MUTATION_EVENTS } from '@nonoll/code-snippet/observer';
const createElement = ({ tag = 'div', id = '', style = '', value = '', text = '' }) => {
const doc = window.document;
const target = doc.createElement(tag);
target.setAttribute('id', id);
target.setAttribute('style', style);
target.setAttribute('value', value);
if (text) {
target.textContent = text;
}
return target;
}
let observer;
const forExample = () => {
if (observer) {
console.log('already example');
return;
}
const doc = window.document;
const target = createElement({ id: 'example_target', style: 'border: 1px solid red' });
doc.body.appendChild(target);
const options = {
childList: true,
subtree: true
};
observer = new MutationObserver({ target, options });
observer.on(MUTATION_EVENTS.WILD_CARD, (type, values) => {
console.log('wildCard', type, values);
}).on(MUTATION_EVENTS.CHANGE_CHILD_LIST, values => {
console.log('childList', values);
});
observer.attach();
const attachButton = createElement({ tag: 'button', text: 'observer attach' });
const detachButton = createElement({ tag: 'button', text: 'observer detach' });
const appendButton = createElement({ tag: 'button', text: 'append' });
doc.body.appendChild(attachButton);
doc.body.appendChild(detachButton);
doc.body.appendChild(appendButton);
attachButton.addEventListener('click', e => {
e.preventDefault();
console.log('attachButton clicked');
if (!observer) {
return;
}
observer.attach();
});
detachButton.addEventListener('click', e => {
e.preventDefault();
console.log('detachButton clicked');
if (!observer) {
return;
}
observer.detach();
});
appendButton.addEventListener('click', e => {
e.preventDefault();
console.log('appendButton clicked');
if (!observer) {
return;
}
const input = createElement({ tag: 'input', value: `${+new Date()}` });
target.appendChild(input);
});
}
forExample();
Extends
Methods
-
attach()
-
이벤트 감지 설정
- Source:
Returns:
- Type
- MutationObserver
Example
observer.attach();
-
destroy()
-
destory
- Source:
Returns:
- Type
- MutationObserver
Example
observer.destroy();
-
detach()
-
이벤트 감지 해제
- Source:
Returns:
- Type
- MutationObserver
Example
observer.detach();
-
emit(eventName, values)
-
이벤트 전파
Parameters:
Name Type Argument Description eventNamestring valuesArray.<any> <repeatable>
- Inherited From:
- Overrides:
- Source:
Returns:
- Type
- EventEmitter
-
fire(eventName, values)
-
이벤트 전파
Parameters:
Name Type Description eventNamestring valuesany - Inherited From:
- Overrides:
- Source:
Returns:
- Type
- EventEmitter
-
off(eventName [, listener])
-
이벤트 감지 해제
Parameters:
Name Type Argument Description eventNamestring listenerTypeVoidFunction <optional>
- Inherited From:
- Overrides:
- Source:
Returns:
- Type
- EventEmitter
-
on(eventName [, listener] [, context])
-
이벤트 감지 등록
Parameters:
Name Type Argument Default Description eventNamestring listenerTypeVoidFunction <optional>
noop context* <optional>
- Inherited From:
- Overrides:
- Source:
Listens to Events:
Returns:
- Type
- MutationObserver
-
once(eventName [, listener] [, context])
-
이벤트 1회 감지 등록
Parameters:
Name Type Argument Default Description eventNamestring listenerTypeVoidFunction <optional>
noop context* <optional>
- Inherited From:
- Overrides:
- Source:
Returns:
- Type
- EventEmitter
Type Definitions
-
MutationRecordType
-
Type:
- String
- Source:
- See:
Properties:
Name Type Description childListString attributesString characterDataString subtreeString
Events
-
MUTATION_EVENTS
-
MutationEvent Types
- Source:
Properties:
Name Type Description CHANGE_CHILD_LISTString childList 변경 시점
CHANGE_SUBTREEString subtree 변경 시점
CHANGE_ATTRIBUTESString attributes 변경 시점
CHANGE_CHARACTER_DATAString data 변경 시점
CHANGEString 기타 변경 시점
WILD_CARDString 모든 변경 시점
Listeners of This Event: