views:

27

answers:

2

Hi,
I am using the jQuery.ajax() with some specific data. One of them is an albumid which is generated on the server side if the user choose not to use one of his albums. The problem is that if a user add several photos to a new album then, instead of adding all photos to the same album, it creates several albums with one photo in each.

What is currently working:

  • update an album (using one the user already has)
  • create a new album
  • send back the new album id

I tried to update the album name field according to the new album id sent back on the success event, thinking that in the next ajax call, the attribute data: ({ 'albumid': album.val() )} would then be the new album id, but it keeps using the former value.

I know that this issue can be solved by using async: false, because in this case each ajax call will use the updated value in the album name field, but it totally freezes the browser.

Do you have any trick/suggestion for this issue?


Cheers,
Nicolas.

A: 

Sounds like you have a race condition. Without seeing the code it's hard to tell for sure but album.val() is probably set once when the code is parsed and not evaluated during the success handler.

You can still use aync calls but you have to do them in serial. It's called an Active Object pattern. Setup an array that holds the ajax calls and call one. Each one has, in the success handler, a call to trigger the next one.

AutoSponge
I think you totally understood my situation, but could you provide me an example? Cheers,
Nicolas
A: 

You should use the new value as album.val() seems not the be reevaluated with the new value.

It should be quite easy with jquery to retrieve the new Album value from $("#hiddenId").val()

Considering that the retrieved id is stored in a created hidden field from the ajax request.

zor
I am actually retrieving the new value by doing: `album.val(msg);` on `success` of the ajax call. I don't see what I can do more in this direction to refresh the album.val(). Plus I think that as all images are requested to be uploaded at the same time (`async: true`), and I only update the `album.val()` when the first one is requested to be uploaded, the others cannot see the field updated as it is actually after the time they were called.
Nicolas
It could certainly help if you give us the version of jquery you use and the plugin you use for the album, within a snapshot of your code. Perhaps there can be some "hack" to do
zor
I'm using jQuery 1.4.2, and if by "plugin" you mean the distant server, it is actually facebook, so nothing we can do on this side I guess. Cheers,
Nicolas
So I deduce you're trying to do a Facebook application, which use the facebook api to modify/upload the facebook album ? I think, you should have a look at the website of this guy who started developping kind api accessing Facebook Album ... http://www.davel.fr/techblog/2010/06/plugin-jquery-get-facebook-albums-photos/. Anyway, by plugin i mean, a javascript file which you include within your main html file.
zor
This guy's script has a different purpose, he wants to GET the albums/photos information whereas I want to UPLOAD them. And actually the only issue I'm experiencing is a freezing of the current tab within a browser while images are uploading.
Nicolas