Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

immutable chrome sqlite return objects

I am using a sqlite DB as a storage system for a webapp. I been using the objects that are returned from queries directly in application. For example:

function get_book_by_id(id,successCallback,errorCallback)
{
    function _successCallback(transaction, results)
    {
        if(results.rows.length==0) {successCallback(null);}
        else
        {
            book=results.rows.item(0);
            successCallback(book);
        }
    }
    db.transaction(
        function (transaction) {
            transaction.executeSql("SELECT id,title,content,last_read from books where id=?;",[id], _successCallback, errorCallback);
    });
}

This returns me an object with the given id, all columns are provided as properties. Nice. The problem I just figured out is that all the properties of the result set object are immutable. So for example if I want to change the property 'title' it takes no effect, which in my opinion makes no sense. Example:

get_book_by_id(1,handle,error);
function handle(book)
{
 //THIS DOESN'T WORK, book.title is still what it was.
 book.title=book.title+"more text";

}

I of course can convert all my DB objects into mutable objects, but I rather would not do that.

Is that an expected behavior? Can I request mutable objects?

I am using google chrome 9.0 on Mac OS X.

like image 776
Stan Wiechers Avatar asked Feb 23 '11 16:02

Stan Wiechers


1 Answers

The WebSQL spec doesn't require for the returned item to be sealed, but it's up to the implementation (the spec does require for the item to be an ordered dictionary with the properties in the same order as the columns in your query).

And no, there is no way to explicitly request a mutable object, so you'll want to do something like the convert_to_mutable() approach suggested by Stan.

BTW, assuming you're using a 3rd party library, it probably has a function for this, for example jQuery.extend() or _.extend().

like image 147
Stepan Riha Avatar answered Nov 15 '22 19:11

Stepan Riha