This is possible by making use of LocalDB
. You can make a local mirror of the objects and bind them to the object-table
instead for editing.
In the view xml:
<var name="local_data_set" type="array:data_object" />
<var name="db_data_set" type="array:data_object" />
<object-table label="Nice Table" query="local_data_set" empty-message="Your items will appear here">
<column heading="Name" display="{name}">
<edit-text value="$object.name" on-change="$: updateValue($object, newValue, 'name')" />
</column>
</object-table>
In the JavaScript:
function init() {
view.db_data_set = DB.data_object.toArray();
view.local_data_set = view.db_data_set.map(function(item) {
var local_copy = LocalDB.data_object.create(item); //set all the enumerable fields
Object.keys(LocalDB.data_object.type.belongsTo).forEach(function(relationship_name){
local_copy[field_name + '_id'] = item[field_name + '_id']; //copy all belongs_to_id fields
});
return local_copy;
})
}
function updateValue(record, new_value, field_name) {
record[field_name] = new_value;
//the save is optional depending on the use case
}
function persistToDB() {
var batch = new DB.Batch();
view.local_data_set.forEach(function(local_item, index){
var db_item = view.db_dataset[index];
db_item.setAll(local_item);
Object.keys(LocalDB.data_object.type.belongsTo).forEach(function(relationship_name){
db_item[field_name + '_id'] = local_item[field_name + '_id']; //copy all belongs_to_id fields
});
batch.save(db_item);
});
batch.execute();
}
Be sure to clear the LocalDB periodically to avoid a large build up of objects.