From: Aleksander Ɓukasz Date: Thu, 10 Oct 2013 13:39:02 +0000 (+0200) Subject: smartxml: trigger events on attr change X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/a30654944f1af2db12a3fc1b7b1bfbd223f4f76c smartxml: trigger events on attr change --- diff --git a/src/smartxml/events.js b/src/smartxml/events.js new file mode 100644 index 0000000..c5e84c4 --- /dev/null +++ b/src/smartxml/events.js @@ -0,0 +1,18 @@ +define([ + +], function() { + +'use strict'; + + +var ChangeEvent = function(type, meta) { + this.type = type; + this.meta = meta; +}; + + +return { + ChangeEvent: ChangeEvent +}; + +}); \ No newline at end of file diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 477166b..bac53af 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -1,6 +1,8 @@ define([ - 'libs/jquery' -], function($) { + 'libs/jquery', + 'libs/backbone', + 'smartxml/events' +], function($, Backbone, events) { 'use strict'; @@ -38,6 +40,11 @@ $.extend(DocumentNode.prototype, { } node.append(this); }, + + triggerChangeEvent: function(type, metaData) { + var event = new events.ChangeEvent(type, $.extend({node: this}, metaData || {})); + this.document.trigger('change', event); + }, }); var ElementNode = function(nativeNode, document) { @@ -74,7 +81,9 @@ $.extend(ElementNode.prototype, DocumentNode.prototype, { }, setAttr: function(name, value) { + var oldVal = this.getAttr(name); this._$.attr(name, value); + this.triggerChangeEvent('nodeAttrChange', {attr: name, oldVal: oldVal, newVal: value}); }, getAttrs: function() { @@ -178,7 +187,7 @@ var Document = function(xml) { return $document[0]; }}); }; -$.extend(Document.prototype, { +$.extend(Document.prototype, Backbone.Events, { ElementNodeFactory: ElementNode, TextNodeFactory: TextNode,