Example #1
0
function updateDom(data) {
    var stored, activeEventNames, activeElement;

    function updateActiveEventsForDataAttributes(el) {
        Object.keys(el.dataset)
        .forEach(updateActiveEventsForDataAttribute);
    }

    function updateActiveEventsForDataAttribute(propName) {
        if (eventNames.indexOf(propName) > -1 && activeEventNames.indexOf(propName) < 0) {
            activeEventNames.push(propName);
        }
    }

    function listenTo(eventName) {
        stored.eventListeners[eventName] = eventListener.bind(null, data.key, eventName);
        stored.target.addEventListener(eventName, stored.eventListeners[eventName]);
    }

    function unlistenTo(eventName) {
        stored.target.removeEventListener(eventName, stored.eventListeners[eventName]);
        delete stored.eventListeners[eventName];
    }

    function updateEventBinding(eventName) {
        if (activeEventNames.indexOf(eventName) > -1 && stored.eventListeners[eventName] == null) {
            listenTo(eventName);
        } else if (activeEventNames.indexOf(eventName) < 0 && stored.eventListeners[eventName] != null) {
            unlistenTo(eventName);
        }
    }

    stored = store[data.key];

    // 1) Patch DOM

    stored.target = patch(stored.target, data.diff);

    // 2) Update DOM event bindings

    activeEventNames = [];

    concat.apply(stored.target, stored.target.getElementsByTagName('*'))
    .forEach(updateActiveEventsForDataAttributes);

    eventNames.forEach(updateEventBinding);

    // 3) Focus element if needed

    activeElement = stored.target.querySelector('[autofocus]');

    if (activeElement != null && document.activeElement !== activeElement) {
        activeElement.focus();
    }
}
Example #2
0
 requestAnimationFrame(() => {
   applyPatch(rootElement, payload)})}
Example #3
0
 .then(patch => applyPatch(monsterMovesDiv, patch))
Example #4
0
 .then(patch => applyPatch(detailView, patch))