• 1 reply
  • Latest Post - ‏2013-01-11T13:10:29Z by SystemAdmin
2327 Posts

Pinned topic searchFiled in JSONStore: Does Worklight cast data type automatically?

‏2013-01-11T09:33:20Z |
Worklight: 5.0.5

In the case,
1: I set datatype in searchFileds to 'boolean'
searchFileds = {key:'boolean'}
2: and add data like this
3: then get data by find method

Is worklight supposed to cast data to 'boolean' type?

seems like not working...
  • SystemAdmin
    2327 Posts

    Re: searchFiled in JSONStore: Does Worklight cast data type automatically?

    The data types you pass to as search fields to initCollection (var searchFields = {name: 'string', age: 'number'}) are just suggestions that may affect how we store data, if you need validation you should implement yourself.

    There's a function called 'enhance' that you can use to create a new add method that does type casting:

    Here's an example:

    var collection = WL.JSONStore.initCollection(...);   collection.enhance(
    'addAndTypeCast', function (data, options) 
    //I assumed data is an object with the name key   
    //Check if we need type casting 
    if (typeof !== 
    //Do type casting =;   
    //Note: should have the datatype you set 
    //for the name key when you called initCollection 
    //When you're done, delegate to the JSONStore add method this.add(data, options); 
    //This is how you would use your new method: collection.addAndTypeCast(
    {onSuccess: function (data) 
    { console.log(
    'Done, stored: ' + data);   
    //If you want to get the data back, use find collection.findAll(
    {onSuccess: function (results) 
    //Results should have 'true' (string) instead of true (boolean) console.log(
    'Results: ' + JSON.stringify(results)); 
    //Note: I did not test the code above, use at your own risk.

    If you feel this lacks a feature you need, feel free to request that feature: