Tuesday, April 29, 2008

Deleting assets using triggers and ajax

There has been a few postings on the MySource Matrix forums about how to delete an asset using a trigger, but the posts are scattered and not comprehensive. So, I thought I would post the way that I remove assets using a trigger, ajax to "touch" the url, and javascript to hide the asset from view.


→ Take a look at the video: Delete asset using triggers/ajax


Our first task is to set up a trigger that will delete an asset from a specific location. In my case, I want to only delete assets that are in the "All Announcements" folder. Set up the trigger using the following:


Event: Asset Accessed



Conditions: Tree Location, URL Matches, Access Permission (You can change this to admin if needed)



Action: Create Link (Link asset into the trash)



Then we need to use some javascript to make it all happen (jQuery is being used):



function deleteAjaxAsset(message, action, assetid, asseturl) {
var confirmDelete = confirm(message);
if (confirmDelete == true) {
if(action == "delete ") {
//Add jQuery function to hide the asset from the users view
$( "# " + assetid).hide();
$( "#announceItemFull ").html( " ");
//Use ajax to post the URL so that our trigger will delete the asset
$.ajax( {
type : "POST ", url : asseturl + "?action=delete " }
);
}
}
}


I am sure this script could be improved apon, but you can extened it to work with more triggers by just adding another if statement with something like action == archive or action == live and create triggers accordingly.


Then we need to call that function with an event, and pass the necessary arguments:


<input type="button" value="Delete Announcement ID:%asset_assetid%" 
id="subBtn%asset_assetid%" onclick="deleteAjaxAsset('Are you sure you want to
delete asset #%asset_assetid%?','delete','announceHubItem%asset_assetid%','%asset_url%');" />

We are passing 4 different things to the function. The message you want to appear, the action, which is delete, the id of the div that you want to hide, and finally, the asset URL.


2 comments:

Hi Nic,

Good to see someone "stretching" Matrix and letting others in on the secrets - thanks.

Your links to your videos arent working though.

Steve

Sorry about that. I moved hosts. They have now been remapped, and should work.

Post a Comment

Newer Post Older Post Home