Giter VIP home page Giter VIP logo

dom-observer's Introduction

observer

Observes a DOM Element property for changes with diff support. Uses xdiff for diff output.

install

$ component install jwerle/observer

usage

var observer = require('dom-observer')
	,	ob1 = observer($('#my-el')[0], {prop: 'style'})

obj.on('change', function (diff, prev) {
	console.log(diff); 
	/**
		A diff for a property set would look like this =>
		[
			[
				"set", // action
				["root", "0" ], // property path .0
				"display" // value
			],
			[
				"set", // action
				["root", "length"], // property path .length
				1 // value
			],
			[
				"set", // action
				["root", "cssText"], // property path .cssTest
				"display: block;" // value
			],
			[
				"set", // action
				["root", "display"], // property path .display
				"block" // value
			]
		]
	**/
});

api

observer(el, opts)

  • el - A DOM Element
  • opts - An object of options where .prop is the property is to observe for changes required
    • prop - The property to observe for changes
    • autoStart - Boolean whether to auto start by default. (default: true)
    • interval - A number in milliseconds for how often to check for changes (default: 50)

example

observer(document.getElementById('el'), {prop: 'dataset'}).on('change', function (diff) {
	// do something with diff
});

#start(interval)

Starts observing at a given optional interval. This is started by default, pass autoStart: false to opts object in constructor. Emits the 'start' event when started.

#stop()

Stops observing. Emits the 'stop' event when stopped.

events

'change'

Emitted when a change occurs on an observed property with the a diff array and a prev object which was the current state of the observed property before the change

'start'

Emitted when observing has started

'stop'

Emitted when observing has been stopped

license

MIT

dom-observer's People

Contributors

jwerle avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

dom-observer's Issues

performance boost: use native APIs

it'd be much, much faster if you used native methods for observation. depending on the UA, you could use any of the following:

  • MutationObserver (DOM4)
  • MutationEvents (DOM3)
  • onpropertychange (IE)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.