How do I get and set the content of an asset using the JS API?
How do I get and set the content of an asset using the JS API?
Assuming you are talking about Standard pages?
If so, to set content, you have to set it on each individual content container:
Using the setAttribute metadata call.
https://matrix.squiz.net/manuals/web-services/chapters/javascript-api#setAttribute
If you want to get the contents of it, you can use the getAttributes function
https://matrix.squiz.net/manuals/web-services/chapters/javascript-api#getAttributes
Well I asked for Matrix source code, because apparently one of the claim is that it is open source, and Iām still waiting on Squiz to send it to me or give me a link. So in the meantime Iām fixing the search using the JS API and adding a Replace functionality so I can search / replace in content that contains code (css, js, containers, designs). We have a big job of removing all the legacy code that has a bunch of non-relative URLs and removing http://domain.root/ parts of it so we can use https and use the pages over various development domains (eg: test[1-9].mydomain.com). I know Web Dev should have linked all resources by asset id but they havenāt so I have years of legacy code to fix, hence the need for a tool.
Set attribute seems to barf here:
Function:
Call (notice all attributes are there):
Result:
ā¦ asset_id, attr_name and attr_value were provided.
Any pointers?
Ok, the value is an array, that is probably the problem. How can I update a design contents then?
Designs have content attributes as arrays
Do I simply stringify the array?
ā¦ apparently not
Wow, that was far fetched but seems like we needed a PHP-style serialisation:
Success:
Modification:
JS Function:
Well, still no good, JS API said attribute has been successfully set but I cannot see the changes with the _admin interface
Pulling the asset info a second time using the API shows my change but the backend doesnāt.
Any pointers? Do I have to lock / update / unlock for the change to appear in the backend? The doc is seriously lacking a a-z working example.
I donāt think you can easily update design parse files via the JS API at the moment.
Internally thereās a real file thatās updated when the asset is edited in _admin, the ācontentsā attribute and compiled (php) design that are generated from that all need to work in concert, it doesnāt look like thereās an appropriate call you can make off the JS API to update that design file though.
If you really need to do this, Iād try exporting a design file to XML, templating out the bits you need and then using https://matrix.squiz.net/manuals/web-services/chapters/javascript-api#importAssetsFromXML
Iāll pass this around and see if thereās a simpler solution though.
Can you give me a working example of this? There is virtually nothing there https://matrix.squiz.net/manuals/web-services/chapters/javascript-api#importAssetsFromXML
For instance, how do I get the path of the design file? I need a full working example of a design update if this is at all possible. If not, dev guys you should fix the api: if(asst.type_code == ādesignā) die(āNot implemented yet.ā);
The problem with updating the Design file contents using the JS API is that it needs to run a HIPO job after you do it, to create/modify/remove design customisation assets. You will see it if you save the contents of a Design file in the Admin Interface and you have a few design areas in place, the HIPO should start. So using the JS API for updating this is probably not feasible.
I havenāt heard of anyone needing to update the Design file using the JS API in the past, so Iām curious as to why you are needing to do it this way?
Just re-read that. Sounds like youād be best to get Squiz Support to run some sort of a script that can do this for you, but Iām pretty sure you are already in talks with them about it. I think there are also scripts they can run to regenerate parse files after their content has changed, so they are the best guys to chat to.
Yes it appears so. Iām pointing to the fact that we have a product here (Squiz Matrix) published with an API that silently fails on certain operations. This needs fixing.
it needs to run a HIPO job after you do it, to create/modify/remove design customisation assets
Template hierarchy (what you guys call design & customisation) should be updated via internal system triggers. Why do HIPO jobs have to run on the front-end?
Are you able to report this as a bug in Squizmap (which operations fail) for us to investigate further with some easy replication steps?
HIPO jobs were originally developed to process long running jobs in order to not exceed PHP web memory limits when sending requests to the application. There are probably other methods we can implement to support this in different ways so that you can trigger jobs (and polling them for their updates) without having to use a HIPO job, which we are looking into for future releases.
Done: https://squizmap.squiz.net/matrix/11277
There are definitely ways of doing this. Best is to look how other CMS do it. Usually it goes like so:
You need:
- A transaction queue (database table, memcached, whatever). Transactions {id, type, data, state, ts, log}. sate = new
- A queue manager (Periodically looks into queue for transactions, daemon PHP script looked after by upstartd or a cronjob to make sure itās alive). state = picked
- Queue manager dispatches transactions to worker script (Usually PHP, taking a couple of MB of memory per process), which will execute the transaction based on the type and data. state = dispatched
- Worker process does what it has to do in the background. sate = processing. If transaction fails for any reason. state = error, log = āCould not acquire lock on abc ā¦ā. If transaction succeed. state = success
- On the frontend (js_api call), poll for the state of the transaction to be error or success then display message accordingly.
Maybe itās already working like this in Matrix, I havenāt looked at the code yet. But in terms of JS API, it shouldnāt be a problem to do something like: tid = js_api.setAttribute(ā¦); js_api.pollTransaction(tid);
Any idea of a workaround until this is fixed?