CSS & Web Design Forum  

Welcome to the CSS & Web Design Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
WM Media Banner

Supporters
Pos Software - POS hardware and Software to track, control and manage your inventory.
Web Templates - BoxedArt is the most reliable source for unlimited template downloads.

Directory Submission
WL Marketing - Your #1 source for building links

Go Back   CSS & Web Design Forum > Web Design > AJAX, Javascript & DOM
Reply
 
Thread Tools Search this Thread Display Modes
(#1)
Old
shuchow@gmail.com
Guest
 
Default Problem creating a closure - 12-30-2008, 08:22 PM

Hi, all. I'm using Webkit's Client-side Javascript functionality to
create an app. I have a object method where I'm trying to create a
closure:

getMessages: function() {

var container;
var transactionFunction = function (transaction) {
container = 'one';

var successCallback = function(tx, result) {
container = 'two';
}

var errorCallback = function(tx, result) {}

transaction.executeSql('SELECT * FROM message', [], successCallback,
errorCallback);
}

this.systemDB.transaction(transactionFunction);
return container;
}

For those not familiar with the client-side js api, there is a
database connection (this.systemDB in this case) that takes a function
(transactionFunction) that executes sql. Any calls to transcation.
executeSql is part of the transaction. The executeSql function
requires two call backs - one for success and one for failure.

From what I understand, the container variable should be in scope at
least when it's set to 'one' and when it's set to 'two.' However,
it's not being set. When the function is called, it returns
undefined. Any ideas?

(what I'm ultimately trying to do is to return the result variable
back to the caller of this getMessages function)

Thanks for any insight.
Reply With Quote
(#2)
Old
Conrad Lender
Guest
 
Default Re: Problem creating a closure - 12-30-2008, 08:45 PM

On 2008-12-30 22:12, shuchow@gmail.com wrote:
> getMessages: function() {
>
> var container;
> var transactionFunction = function (transaction) {
> container = 'one';
>
> var successCallback = function(tx, result) {
> container = 'two';
> }
>
> var errorCallback = function(tx, result) {}
>
> transaction.executeSql('SELECT * FROM message', [], successCallback,
> errorCallback);
> }
>
> this.systemDB.transaction(transactionFunction);
> return container;
> }

...
> From what I understand, the container variable should be in scope at
> least when it's set to 'one' and when it's set to 'two.' However,
> it's not being set. When the function is called, it returns
> undefined. Any ideas?


The closure itself is fine. I'm not very familiar with the storage
interface in Safari and Chrome, but if the value of |container| is still
undefined when getMessages() returns, maybe the systemDB methods don't
run synchronously. That would make sense for a DB-like interface. You're
probably supposed to handle the |result| from the successCallback
function, not from getMessages().


- Conrad

PS, as far as I know, client-side storage is a feature of the browser
(Safari/Chrome) and its JS implementation (V8, JavaScriptCore,
SquirrelFish), and is independent of the rendering engine (WebKit).
Reply With Quote
(#3)
Old
RobG
Guest
 
Default Re: Problem creating a closure - 12-30-2008, 09:01 PM

On Dec 31, 7:12*am, shuc...@gmail.com wrote:
> Hi, all. *I'm using Webkit's Client-side Javascript functionality to
> create an app. *I have a object method where I'm trying to create a
> closure:
>
> getMessages: function() {
>
> * * * * var container;
> * * * * var transactionFunction = function (transaction) {
> * * * * * * * * container = 'one';
>
> * * * * * * * * var successCallback = function(tx, result) {
> * * * * * * * * * * * * container = 'two';
> * * * * * * * * }
>
> * * * * * * * * var errorCallback = function(tx, result) {}
>
> * * * * * * * * transaction.executeSql('SELECT * FROM message', [], successCallback,
> errorCallback);
> * * * * }
>
> * * * * this.systemDB.transaction(transactionFunction);
> * * * * return container;
>
> }
>
> For those not familiar with the client-side js api, there is a
> database connection (this.systemDB in this case) that takes a function
> (transactionFunction) that executes sql. *Any calls to transcation.
> executeSql is part of the transaction. *The executeSql function
> requires two call backs - one for success and one for failure.
>
> From what I understand, the container variable should be in scope at
> least when it's set to 'one' and when it's set to 'two.' *However,
> it's not being set. *When the function is called, it returns
> undefined. *Any ideas?


Try the iPhone web dev group, they mess around with this stuff a bit:

<URL: http://groups.google.com.au/group/iphonewebdev?hl=en >

There's a thread here that may be of interest:

<URL:
http://groups.google.com.au/group/ip...5d7fa83da87d0f
>



--
Rob
Reply With Quote
(#4)
Old
shuchow@gmail.com
Guest
 
Default Re: Problem creating a closure - 12-31-2008, 03:17 PM

Thanks a lot for the tip and the resources. It does sound plausible
that it's a race condition. I'll check the iPhone web dev group for
more clues.
Reply With Quote
(#5)
Old
Shu Chow
Guest
 
Default Re: Problem creating a closure - 01-28-2009, 03:01 PM

After some fiddling and more research, I found out that Conrad was
100% right. The executeSql command runs asynchronously, and it
doesn't look like there's a way to make it run synchronously.

Is there a way I can rewrite the entire function and the callbacks so
that getMessages doesn't return anything until "container" is
populated with the result set?
Reply With Quote
(#6)
Old
Thomas 'PointedEars' Lahn
Guest
 
Default Re: Problem creating a closure - 01-28-2009, 05:19 PM

Shu Chow wrote:
> After some fiddling and more research, I found out that Conrad was
> 100% right. The executeSql command runs asynchronously, and it
> doesn't look like there's a way to make it run synchronously.
>
> Is there a way I can rewrite the entire function and the callbacks so
> that getMessages doesn't return anything until "container" is
> populated with the result set?


Yes, but I don't think you have to rewrite the callbacks. Asynchronous
(what you have, in pseudo code):

var transaction = {
executeSql: function(query, whatever, fSuccess, fError) {
var x = new XHR();

x.open(method, url, true);

x.onreadystatechange = function() {
if (x.readyState == 4)
{
if (isSuccessful(x.status))
{
fSuccess(x);
}
else
{
fError(x);
}
}
};

x.send(...);
}
};

Asynchronous or synchronous (what you ask for, in pseudo code):

var transaction = {
executeSql: function(query, whatever, fSuccess, fError, bAsync) {
var x = new XHR();

if (typeof bASync == "undefined")
{
bAsync = true;
}
else
{
bAsync = !!bAsync;
}

x.open(method, url, bAsync);

var f = function() {
isSuccessful(x.status) ? fSuccess(x) : fError(x);
};

if (bAsync)
{
x.onreadystatechange = function() {
if (x.readyState == 4)
{
f();
}
};
}

x.send(...);

if (!bAsync)
{
f();
}
}
}

Then call:

transaction.executeSql(
'SELECT * FROM message',
[],
successCallback,
errorCallback,
false);


HTH

PointedEars
Reply With Quote
(#7)
Old
Shu Chow
Guest
 
Default Re: Problem creating a closure - 01-29-2009, 02:36 PM

Thanks, but ajax is not what I'm doing.

executeSql is the function running asynchronously. It's an api
function to execute sql statements against the local database. I
don't define it. executeSql(theSqlStatment, variablesInTheStatement,
successCallback, errorCallback).
Reply With Quote
(#8)
Old
Thomas 'PointedEars' Lahn
Guest
 
Default Re: Problem creating a closure - 01-29-2009, 05:34 PM

Shu Chow wrote:
> Thanks,


Thanks for proper quoting next time.

<http://jibbering.com/faq/#posting>

> but ajax is not what I'm doing.


Are you sure? If yes, you could have been more precise; without context,
those callbacks are crying out for XHR, IMHO.

> executeSql is the function running asynchronously.
> It's an api function to execute sql statements against the local database.


Which can still be implemented with XHR if the DBMS provides a HTTP server.
man SOAP

> I don't define it. executeSql(theSqlStatment, variablesInTheStatement,
> successCallback, errorCallback).


Maybe you can use self-calling window.setTimeout() (or, if not
time-critical, window.setInterval()) to call another API function or access
a property provided by the API that yields the status of the SQL query. You
could then call window.clearTimeout() (or window.clearInterval(),
espectively), after which you populate the document with the retrieved data.


PointedEars
Reply With Quote
(#9)
Old
Shu Chow
Guest
 
Default Re: Problem creating a closure - 01-30-2009, 04:14 PM

On Jan 29, 1:26*pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:

> Are you sure? *If yes, you could have been more precise; without context,
> those callbacks are crying out for XHR, IMHO.


> Which can still be implemented with XHR if the DBMS provides a HTTP server.
> *man SOAP
>


I guess my crime is that I hit the delete button while posting my
message from Google. I didn't include my original statement where I'm
saying I'm using "Client-side Javascript." No, I'm not using any sort
of DBMS server, and callbacks and synchronicity are not exclusive to
xmlhttprequest. This has nothing to do with a connection back to the
server.

The psuedo original code I had:

getMessages: function() {

var container;
var transactionFunction = function (transaction) {
container = 'one';

var successCallback = function(tx, result) {
container = 'two';
}

var errorCallback = function(tx, result) {}

transaction.executeSql('SELECT * FROM message', [],
successCallback,
errorCallback);
}

this.systemDB.transaction(transactionFunction);
return container;

}
Reply With Quote
(#10)
Old
Thomas 'PointedEars' Lahn
Guest
 
Default Re: Problem creating a closure - 01-30-2009, 04:35 PM

Shu Chow wrote:
> Thomas 'PointedEars' Lahn wrote:
>> Are you sure? If yes, you could have been more precise; without context,
>> those callbacks are crying out for XHR, IMHO.

>
>> Which can still be implemented with XHR if the DBMS provides a HTTP server.
>> man SOAP


This is not remotely what I posted. Learn to quote, please.
<http://jibbering.com/faq/#posting>

> I guess my crime is that I hit the delete button while posting my
> message from Google. I didn't include my original statement where I'm
> saying I'm using "Client-side Javascript."


For suitable values of "Javascript", that is implied here; see the FAQ.

> No, I'm not using any sort of DBMS server,


Where does your SQL-compatible database run, then?

> and callbacks and synchronicity are not exclusive to xmlhttprequest.


I know that.

> This has nothing to do with a connection back to the server.


Not even a *local* server?

> The psuedo original code I had:
> [...]


I have read it already, there is no point in reiterating.


PointedEars
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Hosting at Triple.com
vBulletin Skin developed by: vBStyles.com

Printing Company  Link Building Services