GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing maxgraph.github.io — ObjectCodec | @maxgraph/core
Class ObjectCodecGeneric codec for JavaScript objects that implements a mapping between JavaScript objects and XML nodes that maps each field or element to an attribute or child node, and vice versa. Atomic ValuesConsider the following example.
This object is encoded into an XML node using the following.
The output of the encoding may be viewed using GlobalConfig.logger as follows.
Finally, the result of the encoding looks as follows.
In the above output, the foo and bar fields have been mapped to attributes with the same names, and the name of the constructor was used for the node name. BooleansSince booleans are numbers in JavaScript, all boolean values are encoded into 1 for true and 0 for false. The decoder also accepts the string true and false for boolean values. ObjectsThe above scheme is applied to all atomic fields, that is, to all non-object fields of an object. For object fields, a child node is created with a special attribute that contains the field name. This special attribute is called "as" and hence, as is a reserved word that should not be used for a field name. Consider the following example where foo is an object and bar is an atomic property of foo.
This will be mapped to the following XML structure by ObjectCodec.
In the above output, the inner Object node contains the as-attribute that specifies the field name in the enclosing object. That is, the field foo was mapped to a child node with an as-attribute that has the value foo. ArraysArrays are special objects that are either associative, in which case each key, value pair is treated like a field where the key is the field name, or they are a sequence of atomic values and objects, which is mapped to a sequence of child nodes. For object elements, the above scheme is applied without the use of the special as-attribute for creating each child. For atomic elements, a special add-node is created with the value stored in the value-attribute. For example, the following array contains one atomic value and one object with a field called bar. Furthermore, it contains two associative entries called bar with an atomic value, and foo with an object value.
This array is represented by the following XML nodes.
The Array node name is the name of the constructor. The additional as-attribute in the last child contains the key of the associative entry, whereas the second last child is part of the array sequence and does not have an as-attribute. ReferencesObjects may be represented as child nodes or attributes with ID values, which are used to look up the object in a table within Codec. The isReference function is in charge of deciding if a specific field should be encoded as a reference or not. Its default implementation returns true if the field name is in idrefs, an array of strings that is used to configure the ObjectCodec. Using this approach, the mapping does not guarantee that the referenced object itself exists in the document. The fields that are encoded as references must be carefully chosen to make sure all referenced objects exist in the document, or may be resolved by some other means if necessary. For example, in the case of the graph model all cells are stored in a tree whose root is referenced by the model's root field. A tree is a structure that is well suited for an XML representation, however, the additional edges in the graph model have a reference to a source and target cell, which are also contained in the tree. To handle this case, the source and target cell of an edge are treated as references, whereas the children are treated as objects. Since all cells are contained in the tree and no edge references a source or target outside the tree, this setup makes sure all referenced objects are contained in the document. In the case of a tree structure we must further avoid infinite recursion by ignoring the parent reference of each child. This is done by returning true in isExcluded, whose default implementation uses the array of excluded field names passed to the ObjectCodec constructor. References are only used for cells in mxGraph. For defining other referencable object types, the codec must be able to work out the ID of an object. This is done by implementing Codec.reference. For decoding a reference, the XML node with the respective id-attribute is fetched from the document, decoded, and stored in a lookup table for later reference. For looking up external objects, Codec.lookup may be implemented. ExpressionsFor decoding JavaScript expressions, the add-node may be used with a text
content that contains the JavaScript expression. For example, the following
creates a field called foo in the enclosing object and assigns it the value
of
The resulting object has a field called foo with the value "myValue" (assuming that
This means the expression is evaluated at decoding time and the result of the evaluation is stored in the respective field. Valid expressions are all JavaScript expressions, including function definitions, which are mapped to functions on the resulting object. Expressions are only evaluated if allowEval is true. Hierarchy (View Summary)
|
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |