Data access
If you need values in your own code, use the SWAC Model. You will get WatchableSets. You can simply modify the attributes as allways but you will have the advantage of databinding. Modify in your code - automatic see results in components.
Access data from component
If you need data, that is allready loaded by a component. You can access those data over the component object.
Be aware, that if you modify the content of a dataset, it will change the data in the component, too.
// Wait for component to be ready
window.swac.reactions.addReaction(function (requestors) {
// Get the component element
let mycompelem = document.querySelector('#mycomp');
// If you are inside a reaction, you can use also use the requestors parameter
mycompelem = requestors["present_example1"];
// Get the component data array
let datasources = mycompelem.swac_comp.data;
// Get a datasource that name you know (else iterate through the array)
let mydatasource = datasources['mydatasource'];
// Get sets
let mydatasets = mydatasource.getSets();
// Access set with it 2
let set2 = mydatasets[2];
}, "present_example1");
Load data with DataRequestor
You can access data by useing window.swac.Model.load(). The
object given to Model.load() is a DataRequestor.
It can contain one of the sources configured in configuration
or global variable name or URL.
SWAC tries to interpret the response as a list of datasets.
If that is possible, the datasets are delivered as array.
If result cannot be interpreted as dataset(s) thet the value is
returned in a single set object with the single attribute "origresponse"
// Get the model
let Model = window.swac.Model;
// Request data
let dataPromise = Model.load({
fromName: 'mytablename', // Name of the datatable
fromWheres: {
filter: 'id,lt,10', // Filter datasets (when use TreeQL / SmartData)
// Note: When you need to use two or more filters, add them in the filter attribute: id,gt,5&filter=id,lt,10
other: 'This is another attribute to send with request'
},
fromHeaders: {
X-Requested-By": "SWAC" // Send headers along the request
},
idAttr: 'id', // Name of sets attribute that contains the id
attributeDefaults: new Map(), // Map of attributname / value for default values when the attribute is missing
attributeRenames: new Map(), // Map of set attributename / wished attributename for renameing attributes
reloadInterval: 10000, // Time in milliseconds after that the data should be refetched from source
});
// Wait for data to be loaded
dataPromise.then(function(data) {
// Direct access dataset with id 1
let set = data[1];
// Iterate over available datasets
for(let curSet of data) {
// Make something with the dataset here
}
}).catch(function(err) {
// Handle load error
});
// Edit dataset
set.myattr = 'new value';
// Done every component will be informed about the new value
Load data with reference
You can access data by useing window.swac.Model.getFromReference(). This gives you the opportunity to get data from references found in another datasets.
// Get the model
let Model = window.swac.Model;
// Specify parameters
let reference = 'ref://some.of.this/format?filter=id,gt,5';
// Each parameter below is optionally
let idAttr = 'id', // Name of sets attribute that contains the id
let attributeDefaults = new Map(), // Map of attributname / value for default values when the attribute is missing
let attributeRenames= new Map(), // Map of set attributename / wished attributename for renameing attributes
let reloadInterval = 10000, // Time in milliseconds after that the data should be refetched from source
// Request data
let dataPromise = Model.getFromReference(reference,idAttr,attributeDefaults,attributeRenames,reloadInterval);
// Wait for data to be loaded
dataPromise.then(function(dataCapsule) {
// Direct access dataset with id 1
let set = dataCapsule.data[1];
// Iterate over available datasets
for(let curSet of dataCapsule.data) {
// Make something with the dataset here
}
}).catch(function(err) {
// Handle load error
});
// Edit dataset
set.myattr = 'new value';
// Done every component will be informed about the new value
Add data to component (from reference)
You can load data from a reference directly into an component. The component is than automatically observer of each dataset the source.
let req = document.getElementById('present_from_reference');
req.swac_comp.addDataFromReference("ref://../../data/exampledata_list.json?filter=id,gt,3&filter=id,lt,7&storage=smartmonitoring");
Add data to component (from javascript objects)
To add data from javascript objects use the addData() method wit the name of the datasource, and the array of sets.
let req = document.getElementById('present_from_jsobjects');
let datasets = [{
name: 'Testvalue 1',
value: 12.2
},
{
name: 'Testvalue 2',
value: 24.4
}];
req.swac_comp.addData('myCustomSource', datasets);
Remove data from component
To remove all data from a component, simply use the removeAllData() or removeData(source) methods available on every component.
let req = document.getElementById('datapresent');
req.swac_comp.removeAllData();
Watch single sets
You can register own code to watch changes in datasets.
// Simple observer
let observer = {
notify: function(set, attrName, attrValue) {
// Get name of modified and new value
}
};
// Request data
let loadProm = Model.load({fromName: 'mytablename'});
loadProm.then(function(data) {
// Get dataset with id 1
let set = data[1];
// Register observer
set.addObserver(observer,'watchedattributename');
// Done. You will be notified on data updates
});
Watch all sets
You can watch all sets you requested with your own code useing model.loadData() or model.getFromReference()
// Simple observer
let observer = {
notify: function(set, attrName, attrValue) {
// Get name of modified and new value
}
};
// Get the model
let Model = window.swac.Model;
// Request data
let dataPromise = Model.load({
fromName: 'mytablename', // Name of the datatable
fromWheres: {
filter: 'id,lt,10', // Filter datasets (when use TreeQL / SmartData)
other: 'This is another attribute to send with request'
},
fromHeaders: {
X-Requested-By": "SWAC" // Send headers along the request
},
observers: [observer]
});
// Wait for data to be loaded
dataPromise.then(function(dataCapsule) {
// Edit dataset with id 1
dataCapsule[1].myattr = 'new value';
// Done every component will be informed about the new value
}).catch(function(err) {
// Handle load error
});
Useing the getFromReference the array of observers is the last parameter:
let dataPromise = Model.getFromReference(reference,idAttr,attributeDefaults,attributeRenames,reloadInterval,observers);
Save data
To save datasets, use the Model.save() method. If your dataset contains an id, the dataset will be updated, else it will be created.
// Get the model
let Model = window.swac.Model;
// Build dataCapsule
let dataCapsule = {
fromName: 'nameofyourdatasource'
};
dataCapsule.data = [{ // Creates a new dataset
name: 'Elia Neumeier',
age: 34
}];
// Request data (returns promise)
Model.save(dataCapsule).then(function(dataCaps) {
// Possible returns rsults from more than one save target
let saveTarged0 = dataCaps[0]; // Is a dataCapsule with fromName and data-Array.
// Access saved dataset of savetarget0
let firstSet = dataCaps[0].data[0];
// If REST interface only response with an id, you find it in data[0].id
let firstSetId = dataCaps[0].data[0].id;
}).catch();
Update data
Updateing data is done with the Model too.
// Get the model
let Model = window.swac.Model;
// Build dataCapsule
let dataCapsule = {
fromName: 'nameofyourdatasource'
};
dataCapsule.data = [
{
id: 2, // Updates dataset with id 2
name: 'Hans Test',
age: 32
}
];
// Request data (returns promise)
Model.save(dataCapsule).then().catch();
Delete data
Deleteing data is is the same way.
// Get the model
let Model = window.swac.Model;
// Build dataCapsule
let dataCapsule = {
fromName: 'nameofyourdatasource'
};
dataCapsule.data = [{
id: 2, // Deletes dataset with id 2
}];
// Request data (returns promise)
Model.delete(dataCapsule).then().catch();
Data creation
to create your own data that is automatic synchronising over differend components, create a WatchableSource and WatchableSets.
Create a WatchableSource
A WatchableSource is observeable and auto observed by components, that use date of that source.
let ws = window.swac.Model.createWatchableSource({fromName: 'sourcename', requestId: 'sourcename?filter=id,es,1'});
Create a WatchableSet
WatchableSets are observeable and inform the components they are used in about changes of data values.
let set = window.swac.Model.createWatchableSet(dataobject);