The article tells you about dhtmlXDataStore, the component which is basically designed to perform a function of data store. But at the same time, it's a full-fledged dhtmlx component with its own API.
To use the functionality of dhtmlXDataStore, you need to include one file:
<script type="text/javascript" src="../codebase/datastore.js"></script>
You have 2 ways to initialize and fill the DataStore:
var myDataStore = new dhtmlXDataStore(); myDataStore.parse([ {id:"1", name:"Accounts Department"}, {id:"2", name:"Customer Service"}, {id:"3", name:"Developing Department"} ]);
var data = new dhtmlXDataStore({ url:"data/data.json", datatype:"json" });
Data scheme allows to set a default scheme for data records of dhtmlXDataStore. So, in cases when you add an empty record to dhtmlXDataStore (data.add({})), the record will be populated with the values set by the scheme.
data.scheme({ name:"Unknown", gender:"male", age:25, department: "Unknown" }); data.add({});//will add { name:"Unknown", gender:"male", age:25, department:"Unknown"} record
Also, there are 2 special keys you can use in a scheme:
myDataStore.data.scheme({ $init:function(obj){ obj.value = obj.name;// 'name' is the name of the column from the db obj.text = obj.name; } });
myDataStore.data.scheme({ $update:function(){ obj.text = obj.name + " " + obj.number; } });
Components, bound to dhtmlXDataStore will keep their data synchronized with the data stored in dhtmlXDataStore. They will also be bound with each other (e.g. grid and form).
var list1 = new dhtmlXDataStore({ url:"../data/data1.json", datatype:"json" }); grid.sync(list1);
var list1 = new dhtmlXDataStore({ url:"../data/data1.json", datatype:"json" }); var list2 = new dhtmlXDataStore({ url:"../data/data2.json", datatype:"json" }); list2.bind(list1, function(data, filter){ return data.value == filter.value; });
var list1 = new dhtmlXDataStore({ url:"../data/data1.json", datatype:"json" }); var list2 = new dhtmlXDataStore({ dataFeed:"../data/json.php", datatype:"json" }); list2.bind(list1, function(data, filter){ filter.Package = "Filter by "+data.Package; }); grid1.sync(list1); grid2.sync(list2);
See also Data Binding in Code Samples.
You probably find it hard to think of situations where this “invisible store” would be useful.
So, let's consider as example some company. It has some number of departments. Each department contains its employees.
Let's assume we want to present on a page the following:
Clearly, we need several components to achieve this.
The final set of components can very, but we've chosen the following:
This stage we divided into 4 steps:
var layout = new dhtmlXLayoutObject(...); var myGrid = layout.cells("c").attachGrid(); var myForm = layout.cells("b").attachForm(); var myCombo = new dhtmlXCombo(...);
var employees = new dhtmlXDataStore(); var departments = new dhtmlXDataStore();
myCombo.sync(departments);// to load data to the combo myGrid.sync(employees); // to load data to the grid
myGrid.bind(myCombo, function(data, filter){ // to link the grid with the combo return myGrid.cells(data, 2).getValue() == filter.text; }); myForm.bind(myGrid);// to link the form with the grid
As you can see we needed just 4 commands to load data and link the components (steps 3 and 4), that proves the stated above: dhtmlXDataStore is realy a handy way to store data while dealing with several components.