From a30654944f1af2db12a3fc1b7b1bfbd223f4f76c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Thu, 10 Oct 2013 15:39:02 +0200 Subject: [PATCH 1/1] smartxml: trigger events on attr change --- src/smartxml/events.js | 18 ++++++++++++++++++ src/smartxml/smartxml.js | 15 ++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 src/smartxml/events.js 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, -- 2.20.1