r98382 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r98381‎ | r98382 | r98383 >
Date:23:05, 28 September 2011
Author:catrope
Status:deferred
Tags:
Comment:
Implement es.DocumentModel.newFromPlainObject and fix the es.DocumentModel constructor
Modified paths:
  • /trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js
@@ -9,8 +9,7 @@
1010 * @param {Array} data Model data to initialize with, such as data from es.DocumentModel.getData()
1111 */
1212 es.DocumentModel = function( data ) {
13 - var data = $.isArray( data ) ? data : [];
14 - return $.extend( data, this );
 13+ this.data = $.isArray( data ) ? data : [];
1514 };
1615
1716 /* Static Methods */
@@ -126,10 +125,6 @@
127126 /*
128127 * SCRATCH CODE
129128 *
130 -es.DocumentModel.newFromPlainObject = function( obj ) {
131 -
132 -};
133 -
134129 es.DocumentModel.prototype.toPlainObject = function() {
135130
136131 };
@@ -173,6 +168,36 @@
174169 };
175170 */
176171
 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+
177202 /*
178203 * Example of content data
179204 *

Status & tagging log