不可变的chrome sqlite返回对象 [英] immutable chrome sqlite return objects
问题描述
我将一个sqlite数据库用作webapp的存储系统。我一直在使用直接在应用程序中从查询返回的对象。例如: 这会返回给我一个给定id的对象,所有列都以属性的形式提供。尼斯。我刚才发现的问题是,结果集对象的所有属性都是不可变的。因此,例如,如果我想更改属性标题,它不会起作用,在我看来这没有任何意义。例子: 我当然可以将所有的DB对象转换为可变对象,但我宁愿不这样做。 这是一种预期的行为吗?我可以请求可变对象吗? 我在Mac OS X上使用google chrome 9.0。 WebSQL规范不会要求,以便将返回的项目密封起来,但是它取决于实现(规范要求项目是一个有序的字典,其属性的顺序与查询中列的顺序相同) / p> 不,没有办法显式地请求一个可变对象,所以你需要做一些像 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: 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: 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 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屋!
$ 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
get_book_by_id(1,handle,error);
函数句柄(书)
{
//这不起作用,book.title仍然是这样。
book.title = book.title +more text;
convert_to_mutable()$如果你使用的是第三方库,它可能有这个功能,例如 jQuery.extend()或 _。extend()。
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);
});
}
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";
}
convert_to_mutable()
approach suggested by Stan.