Scripted asset creation order


(Mahearnpad) #1

(I've also put this topic in support forum - sorry for the duplicate - belongs here better)


I'm creating a series of folder assets using the js api. These folders are actually named A,B,C … Z (to be subsequently used as an A-Z asset listing). I'm passing the name of each folder to the createAsset method, by recursing through an array. What's happening then (and this is probably down to the asynchronous nature of the request/response process) is that the assets are not getting created in A-Z order - they are all over the place.



The client requires them to be in alpha order in the asset map. Is there any way to get the assets created in the correct order?


(Anthony Barnes) #2

Ah, you've hit a problem common to Ajax interfaces. You need to run consecutive callbacks:

    
    function createFolders(parentId,folderArray,callback) {
    if (folderArray.length >= 1 ){
        createAsset(parentId, 'folder', folderArray[0], 1, '', -1, 0, 0, null, null, function(data){
            folderArray.shift(); // Move to next element
            createFolders(parentId,folderArray,callback);
        });
    } else {
        callback.call(this);
    }
    }
    
    var folders = ['A','B','C','D'];
    createFolders(123,folders,function(){
       // Script to process after folder creation
    });


That's untested code, but it should give you a starting point.

(Mahearnpad) #3

[quote]
Ah, you've hit a problem common to Ajax interfaces. You need to run consecutive callbacks:


    
    function createFolders(parentId,folderArray,callback) {
    if (folderArray.length >= 1 ){
        createAsset(parentId, 'folder', folderArray[0], 1, '', -1, 0, 0, null, null, function(data){
            folderArray.shift(); // Move to next element
            createFolders(parentId,folderArray,callback);
        });
    } else {
        callback.call(this);
    }
    }
    
    var folders = ['A','B','C','D'];
    createFolders(123,folders,function(){
       // Script to process after folder creation
    });


That's untested code, but it should give you a starting point.
[/quote]

Thanks Anthony - got it now.