;(function () { var _ = this._ || require('underscore'); var $ = this.jQuery || require('jquery'); var Backbone = this.Backbone || require('backbone'); Backbone.$ = $; function Watchcss(el, props) { this.el = el; props || (props = []); this.props = props; var propsModel = new (Backbone.Model.extend())(_.object(props, [""])); var propschanges = []; propsModel.on('change', function (model) { _(model.changed).each(function (val, propName) { propschanges.push({ propName: propName, oldValue: model._previousAttributes[propName], newValue: val }); }); }); this.mo = new MutationObserver(function (muts) { var i = muts.length; while (i--) { if (muts[i].attributeName !== 'style') continue; // only style mutation propschanges = []; // empty var mut = muts[i]; var j = this.props.length; while (j--) { var prop = this.props[j]; propsModel.set(prop, mut.target.style[prop]); } // 'change' event if (propschanges.length) { this.trigger('change', propschanges); // 'change:' event var l = propschanges.length; while (l--) { var propchange = propschanges[l]; this.trigger('change:'+propchange.propName, propchange); } } } }.bind(this)); this.observe(); } _.extend(Watchcss.prototype, Backbone.Events); Watchcss.prototype.observe = function () { this.mo.observe(this.el, {attributes: true}); return this; }; Watchcss.prototype.disconnect = function () { this.mo.disconnect(); return this; }; window.Watchcss = Watchcss; $.fn.watchcss = function (props) { props || (props = []); var $el = this; var instance = $el.data('watchcss'); if (instance) { instance.props = _.uniq(instance.props.concat(props)); } else { instance = new Watchcss($el[0], props); $el.data('watchcss', instance); } return instance; } }).call(this);