Creating a new data type

Problem

How to create your own data type?

Solution

DataDriver is an object that defines a data type.
DHX gives at your disposal 5 main drivers:

  • json
  • xml
  • html
  • csv
  • jsarray

Therewith, you can specify your own data type - create your own driver.

The structure of creation is the following:

dhx.DataDriver.some={ //some - the name of the type
	toObject:function(text,xml){
                ...
		return text; 
	},
	getRecords:function(data){ 
                var result = [];
                ...
		return result;
	},
	getDetails:function(data){
                var result = {}
                ... 
		return result;
	},
	getInfo:function(data){
		return { 
		 _size:some, 
		 _from:other 
		};
	}
};

For a start, we have some data that we want to use as a new data type. The first thing we need to make with this data - conversion into an intermediate object (an object that will be used as an input parameter in the other functions).
So, our first step - toObject(text, xml) function. The function is called after data loading or directly in the parse method and returns the intermediate data object.

  • text - incoming data.
  • xml - xml object (for xml loading scenario. For other data types can be ignored).

Then, we form an array of records using data from our intermediate object.
The second step - getRecords(data) function.

  • data - the intermediate object from the previous step.

Having an array of records we need to specify a set of properties for each single record.
The third step - getDetails(data).

  • data - an element of the array from the previous step.

The last action has sense just for dynamic data loading scenarios. It's a function that gets count of data and position at which new data needs to be inserted.
The fourth step - getInfo(data). The function returns count of data and the appropriate position mentioned above: '0' if a value is unknown or unnecessary.

  • data - the intermediate object from the previous step.

So, making these four or even three steps you can specify any data type and then use it while developing.