Index: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js |
— | — | @@ -9,8 +9,7 @@ |
10 | 10 | * @param {Array} data Model data to initialize with, such as data from es.DocumentModel.getData() |
11 | 11 | */ |
12 | 12 | es.DocumentModel = function( data ) { |
13 | | - var data = $.isArray( data ) ? data : []; |
14 | | - return $.extend( data, this ); |
| 13 | + this.data = $.isArray( data ) ? data : []; |
15 | 14 | }; |
16 | 15 | |
17 | 16 | /* Static Methods */ |
— | — | @@ -126,10 +125,6 @@ |
127 | 126 | /* |
128 | 127 | * SCRATCH CODE |
129 | 128 | * |
130 | | -es.DocumentModel.newFromPlainObject = function( obj ) { |
131 | | - |
132 | | -}; |
133 | | - |
134 | 129 | es.DocumentModel.prototype.toPlainObject = function() { |
135 | 130 | |
136 | 131 | }; |
— | — | @@ -173,6 +168,36 @@ |
174 | 169 | }; |
175 | 170 | */ |
176 | 171 | |
| 172 | +es.DocumentModel.newFromPlainObject = function( obj ) { |
| 173 | + /* |
| 174 | + * Flatten a node and its children into a data array, recursively. |
| 175 | + * |
| 176 | + * @param obj {Object} A plain node object //TODO where do we document this whole structure? |
| 177 | + * @return {Array} Array to append the flattened version of obj to |
| 178 | + */ |
| 179 | + function flattenNode( obj ) { |
| 180 | + var i, data = []; |
| 181 | + // Open element |
| 182 | + // TODO do we need to copy the attributes object or can we use a reference? |
| 183 | + data.push( { 'type': obj.type, 'attributes': obj.attributes, 'node': null } ); |
| 184 | + if ( obj.content !== undefined ) { |
| 185 | + // Add content |
| 186 | + data = data.concat( es.ContentModel.newFromPlainObject( obj.content ).data ); |
| 187 | + } else { |
| 188 | + // Add children. Only do this if there is no content property |
| 189 | + for ( i = 0; i < obj.children.length; i++ ) { |
| 190 | + //TODO figure out if all this concatting is inefficent. I think it is |
| 191 | + data = data.concat( flattenNode( obj.children[i] ) ); |
| 192 | + } |
| 193 | + } |
| 194 | + // Close element // TODO do we need attributes here or not? |
| 195 | + data.push( { 'type': '/' + obj.type, 'node': null } ); |
| 196 | + return data; |
| 197 | + } |
| 198 | + |
| 199 | + return new es.DocumentModel( flattenNode( obj ) ); |
| 200 | +}; |
| 201 | + |
177 | 202 | /* |
178 | 203 | * Example of content data |
179 | 204 | * |