What would be the best way to store and display the user configuration on a form?
The problem with this is obviously that we want to use as few tables as possible, to store the configuration, ideally only one. However, we have to store different types of data in the same table. We can store all values as varchar and convert them to whatever type we need when we access configuration, that’s ok. But how do we display them all on one screen?
I’m not experienced in that kind of thing and what I came up with is having a form variable for each configuration option (e.g. _maxCodeLength, _defaultTaxCode, _markupSellPriceBy, etc.) and having two methods for each of the variables: one to get the value of the variable from the database table, the other to set this value in the database by sending an UPDATE query. Thus, I have to load all variables when the form is shown and also set all values when the user hits save.
I think this solution is clumsy and there’s a lot of overhead methods and variables.
Could someone, please, give me a hint at what the best way to do this is? I’m researching this topic in the Internet but it’s kind of tricky so far…
I don’t know about displaying, since you will probably need to have one variable per field, but to store/retrieve, you can set a global variable of type object and then access its property (adding them at will and retrieving them very simply), then you can serialize this one object in a text field and deserialize it later (using plugins.serialize fromJSON and toJSON methods).
Something like this:
in global.js:
var solutionPrefs = {};
on a form on the user’s record:
function readPrefs() {
globals.solutionPrefs = plugins.serialize.fromJSON(pref_field);
}
function storePrefs() {
pref_field = plugins.serialize.toJSON(globals.solutionPrefs);
}
after that you can use the object, adding new property is as easy as:
var clientValue = (globals.solutionPrefs.newProperty) ? globals.solutionPrefs.newProperty : null;
Now, to display this you can probably use the solution model to build an entire form on the fly based on the content of the solutionPrefs properties, you can enumerate on it like that:
for (prop in globals.solutionPrefs) {
if (typeof prop == "integer") {
// create a check field
} else if (typeof prop == "number") {
// create a text field, maybe add a validator
} else {
// fall back to create a text field
}
}
That would be portable, will take only one field per user in a preference table and would be extensible at will…
The more I say it, the more I see how it could be some kind of generic module…
I will give it a try and get back to you.
maria:
Thanks Patrick!
The only thing I wouldn’t like to do is creating the form on the fly.
I’ll give it a go and let you know what happened.
Glad it works for you!
I spend some time trying to create the form on the fly. But right now with 4.1.3 the solutionModel has too much bugs and/or limitations to do it: the trick is that you need to create a form and iterate on the properties of the object to create a DataSet “model” and a DataSource for the form, and then create fields and assign those virtual dataProvider to it… but when you assign a dataSource created from memory as the source of a form all sort of weird things happen, like Servoy trying to update a “TEMPXXXXXXX” non existing table…
I tried creating form variables and assign them as dataProvider to the fields to no avail.
I managed to take the same code and make it work on Tano without changing a line, proof that something is broken in the current implementation, but still, when you the try to recreate a dataSource later in the same session (if you add properties to your preferences object for example) the forms try again to update that non-existentTEMP table! When I look in the debugger I have a mem:datasource which seems right but somehow Servoy is choking on it…
So, I will not attempt to create a fully dynamic preference form, and you are right that this is not an easy thing to do, but will see if they can improve the solutionModel to allow these kind of things in Tano.
You can add a case with a small sample about this, seems related to: http://forum.servoy.com/viewtopic.php?f=22&t=12830 . Not sure if that case was fixed or for what version will be fixed (as id is not specified).
lvostinar:
You can add a case with a small sample about this, seems related to: viewtopic.php?f=22&t=12830 . Not sure if that case was fixed or for what version will be fixed (as id is not specified).
Hi Laurian,
Indeed It seems related, I will try to build a sample for this and add a case (and see how it goes in Tano too).
Thanks,
For configuration stuff I cretaed a class BasicConfiguariation with instance variables ‘selectedDomain’, ‘selectedStudent’, ‘defaultSubject’ etc. In an accessor method I return a singleton of this class, stored in a form variable. From all over the application you can save and retrieve current configuration settings.
For now I only have one configuration. But in the future we might distinguish between user and application settings or runtime settings and persistent setting or module and all over settings.
What I ignored so far is the question, how to store and retrieve it from a DB on application startup. I still have some time (months) to think about
Regards
Birgit
PS: It looks as if we care about the same problems
For configuration stuff I cretaed a class BasicConfiguariation with instance variables ‘selectedDomain’, ‘selectedStudent’, ‘defaultSubject’ etc. In an accessor method I return a singleton of this class, stored in a form variable. From all over the application you can save and retrieve current configuration settings.
For now I only have one configuration. But in the future we might distinguish between user and application settings or runtime settings and persistent setting or module and all over settings.
What I ignored so far is the question, how to store and retrieve it from a DB on application startup. I still have some time (months) to think about
Regards
Birgit
PS: It looks as if we care about the same problems
And serialize doesn’t do allow you to store and retrieve from DB?
So far I have no experience with that plugin. Reading the example code I’d guess I need to convert the configuration object into an array (and vice versa). And this can be serialized. II’ll give it a try. Thank you.
birgit:
So far I have no experience with that plugin. Reading the example code I’d guess I need to convert the configuration object into an array (and vice versa). And this can be serialized. II’ll give it a try. Thank you.
Regards
Birgit
Hi Birgit,
No need to convert your configuration file into an array, you can serialize and deserialize any object that you have created in JavaScript, that’s the whole point of Serialization!
will return a String that you can store in your DB, then you retrieve it with
configurationObject = plugins.serialize.fromJSON(dataProviderString);
You can also use the blobSerializer or StringSerializer column converter, then Servoy will do the serialization for you and it is transparent to the developer.
rgansevles:
You can also use the blobSerializer or StringSerializer column converter, then Servoy will do the serialization for you and it is transparent to the developer.