How to use response from Javascript API?


(Kieran@parracity) #1

Hi all,

 

In the documentation: http://manuals.matrix.squizsuite.net/web-services/chapters/javascript-api it mentions for most operations/requests that there is a response and a Custom Callback Function.

 

The callback function is obviously a function that you define and set, however is this only fired ONCE/IF the request is complete, or is it fired after the request is sent?

How do I access the response? If it is accessed through the customcallbackFunction that I set, how would I structure said function to gain the callback? Should these manuals be updated or am I retarded?

 

For arguments sake, let's say I use setMetadata :

js_api.setMetadata({
   "asset_id":100,
   "field_id":900,
   "field_val":"metadata",
   "dataCallback":customDataCallbackFunc
}) 

How would I know if and when this request has been completed (using ajax), as sometimes the metadata hasn't shown the change for up to 20 secs?

Note: this delay could be due to our internal network speed, however I'd still like to be able to utilise a response that fires only when the change has gone through.

 

Thanks all and Nick in advance.


(Cgrist) #2

The callback should be fired when the response is returned from Matrix, and you should get a message indicating whether or not the metadata setting was successful. You migth get something like 'you do not have permission to access this asset' if permissions are not right, for example. 

 

The easiest way to get your head around how it works is to use the JS console in Chrome or Firebug, and see it happening. If you've got a page where you have a JS API object available, you can then test it like this, by typing your code directly into the console and executing it:

 

js_api.setMetadata({    "asset_id":100,
    "field_id":900,
    "field_val":"metadata",
    "dataCallback": function(data){

        console.log(data);
   }

})

 

These days chrome will let you expand the 'data' object that is returned inside the console, so you can see any subelements that it contains (e.g sometimes you'll get a data object that has a 'success' variable inside it, which is set to a string, which includes the success message)

 

If you're debugging an existing implementation, try switching to the 'network' panel in chrome dev tools - you should be able to find the AJAX requests that are made to the JS API. If you click on these you should be able to view the headers that were sent, and the response you get back, which will be a JSON representation of the 'data' object in my previous exmaple, or you might see a network error if it's failing to communicate to the JS API at all.


(Kieran@parracity) #3

^ Champion!

 

Thank you!

 

I will put in a suggestion to the matrix manuals.