不可变的chrome sqlite返回对象 [英] immutable chrome sqlite return objects

查看:164
本文介绍了不可变的chrome sqlite返回对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一个sqlite数据库用作webapp的存储系统。我一直在使用直接在应用程序中从查询返回的对象。例如:
$ b $ pre $ 函数get_book_by_id(id,successCallback,errorCallback)
{
函数_successCallback(事务,结果)
{
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);
});



$ b $ p
$ b

这会返回给我一个给定id的对象,所有列都以属性的形式提供。尼斯。我刚才发现的问题是,结果集对象的所有属性都是不可变的。因此,例如,如果我想更改属性标题,它不会起作用,在我看来这没有任何意义。例子:

  get_book_by_id(1,handle,error); 
函数句柄(书)
{
//这不起作用,book.title仍然是这样。
book.title = book.title +more text;


我当然可以将所有的DB对象转换为可变对象,但我宁愿不这样做。



这是一种预期的行为吗?我可以请求可变对象吗?



我在Mac OS X上使用google chrome 9.0。

解决方案

WebSQL规范不会要求,以便将返回的项目密封起来,但是它取决于实现(规范要求项目是一个有序的字典,其属性的顺序与查询中列的顺序相同) / p>

不,没有办法显式地请求一个可变对象,所以你需要做一些像 convert_to_mutable() jQuery.extend()或 _。extend()


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.

解决方案

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().

这篇关于不可变的chrome sqlite返回对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆