Topic
  • 1 reply
  • Latest Post - ‏2013-01-11T13:10:29Z by SystemAdmin
SystemAdmin
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
myCollection.add({key:'test'},options);
3: then get data by find method

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

seems like not working...
  • SystemAdmin
    SystemAdmin
    2327 Posts

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

    ‏2013-01-11T13:10:29Z  
    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:
    http://pic.dhe.ibm.com/infocenter/wrklight/v5r0m5/index.jsp?topic=%2Fcom.ibm.worklight.help.doc%2Fapiref%2Fr_wl_jsonstore_enhance.html

    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 data.name !== 
    'string') 
    {   
    //Do type casting data.name = data.name.toString();   
    //Note: this.schema.name 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(
    {name: 
    
    true
    }, 
    {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:
    http://www.ibm.com/developerworks/forums/thread.jspa?threadID=461545&tstart=0