Zebra API Doc

zebra.util.Bag Class

JSON configuration objects loader class. The class is handy way to keep and load configuration encoded in JSON format. Except standard JSON types the class uses number of JSON values and key interpretations such as:

  • "@key_of_refernced_value" String values that start from "@" character are considered as a reference to other values
  • { "$class_name":[ arg1, arg2, ...], "prop1": ...} Key names that starts from "$" character are considered as name of class that has to be instantiated as the value
  • {"?isToucable": { "label": true } } Key names that start from "?" are considered as conditional section.

Also the class support section inheritance. That means you can say to include part of JSON to another part of JSON. For instance, imagine JSON describes properties for number of UI components where an UI component can inherits another one.

   {
      // base component  
      "BaseComponent": {
          "background": "red",
          "border": "plain",
          "size": [300, 300]
      },

       // component that inherits properties from BaseComponent,
       // but override background property with own value
      "ExtenderComp": {
          "$inherit": "BaseComponent",
          "background": "green"
      }
   }

The loading of JSON can be multi steps procedure where you can load few JSON. That means you can compose the final configuration from number of JSON files:

   // prepare object that will keep loaded data
   var loadedData = {}; 

   // create bag
   var bag = zebra.util.Bag(loadedData);

   // load the bag with two JSON
   bag.load("{ ... }", false).load("{  ...  }");

Constructor

zebra.util.Bag

(
  • [obj]
)

Parameters:

  • [obj] Object optional

    a root object to be loaded with the given JSON configuration

Methods

contentLoaded

(
  • content
)

Callback method that can be implemented to be called when the bag JSON has been completely loaded but not parsed. The method can be useful for custom bag implementation that need to perform extra handling over the parsed JSON content

Parameters:

  • content Object

    a parsed JSON content

end

()

End loading JSONs and parse final result

get

(
  • key
)
Object

Get a property value. The property name can point to embedded fields:

 var bag = new Bag().loadByUrl("my.json");
 bag.get("a.b.c");

Also the special property type is considered - factory. Access to such property causes a new instance of a class object will be created. Property is considered as a factory property if it declares a "$new" field. The filed should point to a method that will be called to instantiate the property value.

Parameters:

  • key String

    a property key.


Return: Object

a property value

load

(
  • s
  • [b]
)
zebra.util.Bag

Load the given JSON content and parse if the given flag is true. The passed
boolean flag controls parsing. The flag is used to load few JSON. Before parsing the JSONs are merged and than the final result is parsed.

Parameters:

  • s String

    a JSON content to be loaded

  • [b] Boolean optional

    true if the loading has to be completed


Return: zebra.util.Bag

a reference to the bag class instance

loadByUrl

(
  • url
  • [b]
)
zebra.util.Bag

Load JSON by the given URL

Parameters:

  • url String

    an URL to a JSON

  • [b] Boolean optional

    true if the loading has to be completed


Return: zebra.util.Bag

a reference to the bag class instance

mergeContent

(
  • o
  • v
)
Object protected

Merge content of the specified object with the specified value and return a merge result.

Parameters:

  • o Object

    an object with that the value is merged

  • v Object

    a value to be merged


Return: Object

a merge result

resolveClass

(
  • className
)
Function

Called every time the given class name has to be transformed into the class object (constructor) reference.

Parameters:

  • className String

    a class name


Return: Function

a class reference

Attributes

concatArrays

Boolean

The attribute rules how arrays have to be merged if the bag is loaded from few JSON sources. true means that if a two JSON have the same key that reference to array values the final value will be a concatenation of the two arrays from the two JSON sources.

Default: false

usePropertySetters

Boolean

The property says if the object introspection is required to try find a setter method for the given key. For instance if an object is loaded with the following JSON:

    {
       "color": "red"
    }

the introspection will cause bag class to try finding "setColor(c)" method in the loaded with the JSON object and call it to set "red" property value.

Default: true