powered by Authenteo

Persistent JavaScript

Persistent JS Data Structures

Persistent JavaScript defines certain special fields to describe the structural characteristics of objects and their instances. A structure object is the persisted (within an object graph) equivalence of a class. The main difference is that a class has it's value defined in with the code in a text file, and a structure is defined with the same data structures that define the rest of the data. This allows structures to be persisted along with objects and object graphs and structures can be accessed by other languages.

structure field

If an object defines a persistent field named structure, the object that is referenced by the field will hold information about the defined structure of the object. This structure information is similar to the information provided by a class in a class-based language. The structure object can denote what are valid values for each field in the object, and if the object is sealed (no additional fields can be added). Each field of the structure object should reference a field identifier object. The field in the structure object should correspond to the field that in the referring object.

Field Identifer Object Format

A field identifier object should have the following properties:

            {
            

 type : type_definition // optional
             persistent : true | false - indicates if the field is 
          
               persistent or transitive. True by default (optional)
          
             overwritable : true | false | access object, // indicates if 
          
               instances can overwrite this field. True by default (optional)
          
             mutable : true | false | access object //indicates if 
          
               the base field can be modifed. True by default (optional)
            

 description : " description of the field ", // optional
             dontEnum : true | false // indicates if the field should be 
          
               included in enumeration
            

}
             
          
            
              type_definition
            
             = "
            
              primitive
            
            " | 
            
              reference to required class/structure 
            
            

             
          

Implicit Field Definition

Without structural definition, fields have implicit definition. With an existing persisted object, all existing persisted fields will be considered persistent by default and newly created fields will be treated as transient fields. To create a new persisted fields, one should call pjs.set to explicitly define a new persistent property. When a new object is persisted, all the properties of that object should be treated as persistent by default and be persisted. After being persisted, the rules for existing persistent objects should be followed.

Function Field Identifier

A special field identifier may be used to indicate a function:

            {
          
             type : "function",
          
             "return" : 
            
              type_definition
            
             | 
            
              reference to required class/structure
            
            

"params" : [ type_definition, type_definition,... ],
             ... the rest of the field identifier fields are still applicable
          
            }
          
             
          

Other Structure Fields

isArray - indicates that the object should be an array

isSet - indicates that the object should be a set (which can be an array, but indicates that changing the ordering of objects in the array will not be preserved and duplicates will not be preserved).

Persistent Function Format

To define a function in a persistent object graph, the function should be a first class persistent object with a field named "function" that holds the function code. In addition, a persistent function object may optionally have a "scope" field that refers to the parent scope to use for function. For example:

            {id : "1",
            

"function" : "function() { alert(foo); // should display "bar" }",

scope : { foo : "bar"}



}

Getters and Setters

An object may define a field that uses a getter/setter to provide property access and control property modification.

For example:

            object = { id : "1",
          
             prop : {
            

getter : {

method : "function() { return 10 }"

},

setter : {

method : "function(value) { alert(value) }"

}

}

}

If we defined object with this JSPON object definition, executing the expression object.prop would call the getter function and return a value of 10. If we executed object.prop = 20, the setter function would be called and an alert would popup and show the value 20.