From 5b4122c6f8f4fef93526a0029aa6a4ba5f329cfa Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Wed, 23 Oct 2013 12:13:13 +0200 Subject: [PATCH 1/1] smartxml: loading new xml content to an existing document instance --- src/smartxml/smartxml.js | 25 ++++++++++++++++--------- src/smartxml/smartxml.test.js | 9 +++++++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index cb90e74..80ee43c 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -214,16 +214,9 @@ var parseXML = function(xml) { }; var Document = function(xml) { - var $document = $(parseXML(xml)); - - var doc = this; - Object.defineProperty(this, 'root', {get: function() { - return doc.createElementNode($document[0]); - }}); - Object.defineProperty(this, 'dom', {get: function() { - return $document[0]; - }}); + this.loadXML(xml); }; + $.extend(Document.prototype, Backbone.Events, { ElementNodeFactory: ElementNode, TextNodeFactory: TextNode, @@ -239,6 +232,20 @@ $.extend(Document.prototype, Backbone.Events, { return new this.TextNodeFactory(nativeNode, this); }, + loadXML: function(xml) { + var $document = $(parseXML(xml)); + + var doc = this; + Object.defineProperty(this, 'root', {get: function() { + return doc.createElementNode($document[0]); + }, configurable: true}); + Object.defineProperty(this, 'dom', {get: function() { + return $document[0]; + }, configurable: true}); + + this.trigger('contentSet'); + }, + toXML: function() { return this.root.toXML(); } diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index d997aa7..da1b335 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -30,6 +30,15 @@ describe('smartxml', function() { var doc = getDocumentFromXML('
'); expect(doc.root.getTagName()).to.equal('div'); }); + + it('can resets its content entirely', function() { + var doc = getDocumentFromXML('
'); + + expect(doc.root.getTagName()).to.equal('div'); + + doc.loadXML('
'); + expect(doc.root.getTagName()).to.equal('header'); + }); }); describe('Basic ElementNode properties', function() { -- 2.20.1