JavaScript API


(Adam Fisher) #1

Hi, I've been building an asset creator and basically wanted to create an API for managing asset attributes with JavaScript. I want to be able to edit the metadata but I imagine it could be extended to cover a broader range of attributes.


I envisioned it working akin to:

    Asset(asset_id).metadata(section, field).get();
    Asset(asset_id).metadata(section, field).set(value);

and potentially,

    Asset(asset_id).metadata({section1: field1, section2: field2, etc}).get();
    Asset(asset_id).metadata({section1: field1, section2: field2, etc}).set([value1, value2, etc.]);

but of course this could be extended to include

    Asset(asset_id).details...
    Asset(asset_id).pageContent...

and while we're at it, things like

    Asset(asset_id).parent; //return array if more than one
    Asset(asset_id).children; //return array of children

would probably be very useful.



The JavaScript side of it is no problem - it's simple to create a class to take care of all the AJAX requests, but I wonder whether it is possible for Matrix to deal with the information?



This all started because I literally just wanted to be able to edit the metadata from javascript, given an asset_id and the metadata field names/section names… (So if someone can tell me how to do that, I'll go away a happy man!)



Thanks,

Adam



ps. We're on Matrix V3.10.0 here, so I can't use the URL to pass the data to a trigger :frowning:


(Daniel Nitsche) #2

[quote]…


This all started because I literally just wanted to be able to edit the metadata from javascript, given an asset_id and the metadata field names/section names… (So if someone can tell me how to do that, I’ll go away a happy man!)



[right][post=“15878”]<{POST_SNAPBACK}>[/post][/right][/quote]



Can you provide more information on why you want to use JavaScript in the first place?



I ask because you can edit an asset’s metadata through the metadata screen via simple edit.


(Adam Fisher) #3

I'm making an asset builder for a metadata-driven website. Someone's going to have to add literally thousands of pages with all their metadata so I wanted to make it as quick and easy as possible. If I could use JavaScript, then it would be possible to submit all the asset properties in a single hit at the same time as creating the page (rather than having to go through to the metadata edit screen).


It doesn't sound like such a big deal, but the screens can be pretty slow to load and when you are doing so many of them it'll add up.



(I realise this is possible in V3.12 + by using triggers and URL data, but that's unavailable to me here for the foreseeable future.)



In addition, as JavaScript driven websites become increasingly popular (especially in light of the maturation of JavaScript as a language thanks to frameworks like mootools) I reckon this is something that'll come up more and more. Plus it gives developers complete freedom over asset management, instead of being tied to /_edit. I'm happy to work on the problem (especially the JavaScript bits) but really just wanted to sound out the feasibility of the situation.



Perhaps a new AJAX Handler asset that can be configured to accept data from an AJAX script and return the information (obviously, not wrapped in the page headers etc.)?


(Daniel Nitsche) #4

[quote](I realise this is possible in V3.12 + by using triggers and URL data, but that’s unavailable to me here for the foreseeable future.)
[right][post=“15886”]<{POST_SNAPBACK}>[/post][/right][/quote]



3.12 allows you to expose the metadata screen via an asset builder, so in the scenario you’ve outlined, this would be the most appropriate solution.


[quote]In addition, as JavaScript driven websites become increasingly popular (especially in light of the maturation of JavaScript as a language thanks to frameworks like mootools) I reckon this is something that'll come up more and more. Plus it gives developers complete freedom over asset management, instead of being tied to /_edit. I'm happy to work on the problem (especially the JavaScript bits) but really just wanted to sound out the feasibility of the situation.[/quote]



IANAD (I am not a developer), but I'd imagine that this sort of functionality would be much better suited for the next version of MySource (version 4), as it's going to be more JavaScript/DHTML/Ajax based.



Not that I want to discourage you at all :slight_smile:


(Greg Sherwood) #5

There are no plans to introduce an JS API for editing assets in MySource Matrix. We develop those sort of Ajax applications using triggers, as mentioned in this thread.


(Anthony Barnes) #6

Actually, it isn't available until 3.14. There is a new trigger that allows you to set attributes and metadata values from GET and POST variables. It requires you to configure the trigger with specific metadata schema values in mind.

I am about to build similar ajax functionality for a client, but it's not part of any future plans to build a javascript API to matrix - that would be much more involved than the scope of this single project.

MySource 4 may have support for web services which I think should bring us closer to the things you've mentioned in your posts.

(Andrei Railean) #7

If you're well versed with JS, you could probably come up with a way to talk to _edit urls from JS and save user the trouble of doing so. Layouts (simple edit customisation screens) can be coded to have helper javascript. If you apply the same layout to all the assets, you might be able to achieve what you want.



I'm not sure how much you have to play with in terms of layouts and customisation thereof in 3.10.



In short, complete freedom over asset management using JS is not easy to achieve. You've got to fight for it and maybe wait for a newer version of matrix like 3.14.


(Avi Miller) #8

[quote]Actually, it isn’t available until 3.14.
[right][post=“15892”]<{POST_SNAPBACK}>[/post][/right][/quote]



I think Dan is referring to the fact that in v3.12, Asset Builders can expose Metadata fields on the create screen, so that you can create an asset and set its metadata at the same time.


(Daniel Nitsche) #9

[quote]I think Dan is referring to the fact that in v3.12, Asset Builders can expose Metadata fields on the create screen, so that you can create an asset and set its metadata at the same time.
[right][post=“15895”]<{POST_SNAPBACK}>[/post][/right][/quote]



I think Anthony is referring to 3.14’s triggers via Get variables :smiley:


(Adam Fisher) #10

It sounds like this would be perfect for AJAX applications! As I said, I've got no problems with creating the JavaScript to do it, but getting Matrix to understand the submitted data and act on it accordingly would be tricky. I can't think of anything available to me in 3.10 that allows getting/setting data from a form submission.

[edit] actually, I just mean setting. Getting is possible with paint layouts and asset listing pages, obviously[/edit]

I thought about trying to build a custom asset to take care of it, but an asset that can do all that is way beyond me at the moment. (I'm still a Matrix newbie)


[quote]I think Dan is referring to the fact that in v3.12, Asset Builders can expose Metadata fields on the create screen, so that you can create an asset and set its metadata at the same time.[/quote]

This is exactly the functionality I was trying to emulate - it's a shame we're still on V3.10! But it got me thinking so I guess it's a good thing.


[quote]If you're well versed with JS, you could probably come up with a way to talk to _edit urls from JS and save user the trouble of doing so.[/quote]

This was the first thing I had a look at, but the /_edit screens generate some horrendously complicated HTML so it's tough to work out. I did gather that the form requires not only the asset_id of the asset you're editing, but also the asset_id's for parent elements, although I may be mistaken? That pretty much put me off, since I don't think there's way of getting the parent element in Matrix?


[quote]Layouts (simple edit customisation screens) can be coded to have helper javascript. If you apply the same layout to all the assets, you might be able to achieve what you want.[/quote]

This sounds interesting. Can you point me at some documentation on this/give a few little pointers? /_edit screens would make pretty good AJAX handlers if it could work.



Thanks for your input, everyone.


(Adam Fisher) #11

I had a bit of a crack at this last week. Basically, I logged the post request from an edit page, and tried to recreate it with an AJAX call. Unfortunately, the POST request is denied by the server. As far as I can tell, the only difference between the form submission and the AJAX POST I created was the 'Referer' header. Does Matrix check the referer header of incoming _edit requests?

It would make a certain amount of sense if it did, to stop people trying to do exactly what I'm doing, but you still need to pass the session data so it isn't necessarily a security breach.


(Tom Barrett) #12

No, Matrix doesn't check the refer®er. If you're getting login screens in response I would suspect your ajax client is somehow not submitting the session cookie. But I would've thought that browsers preserved cookies as usual in XmlHTTPRequest.


Re your comment "I did gather that the form requires not only the asset_id of the asset you're editing, but also the asset_id's for parent elements, although I may be mistaken?" - I think the lineage is in the form so the asset map knows where it should start off, but you can leave it out without causing any other trouble.


(Eric) #13

I've just stumbled accross this post, is there a tutorial as to how to bring all fields (create and metadata) on one screen?


we using 3.12.3, trying to create asset builder, but I can't find how to do it, can someone point me to a post or just list a keywords. We are creating single calendar assets in only one location with a metadata schema applied to it.



Much appreciated.


(Daniel Nitsche) #14

All you need to do, is select a "Metadata Schemas to Apply" on the details screen of the Asset Builder, then add a %create_form% keyword to your Logged in/Not Logged in bodycopy.


(Eric) #15

Thanks Daniel, but this is open to public so I'd rather make it look as close as possible to normal form then simple edit interface.


(Avi Miller) #16

Do you see the Custom Create Bodycopy options on your Asset Builder? If so, you can use keywords to print the input boxes for the standard asset attributes and metadata fields. In this way, you can customise the look of your create interface completely.

(Eric) #17

That is exactly what I need, the only keywords I can see however are:

  1. Screen menu
  2. Commit button
  3. Release-locks button
  4. All of screen xx
  5. All of selection yy of screen xx
  6. Field zz of screen xx
  7. Custom limbo keyword pp of the current asset type



    Anywhere I can find explanation on what they are? First 4 I understand 6 is also ok



    what does 5 and 7 mean?



    would getting to metadata be something like that:

    %metadata-F_metadata-field-name%



    Thanks for your help.

(Adendle) #18

With Regards to Using an Asset Builder.......

Because you are using an Asset Builder it will not automagically inherit the design styles of your Simple Edit Interface. So this leaves you open to style the asset builder with it's own custom design if you like. This is the approach that we are using for News Asset Builders.....

-=A=-

(Avi Miller) #19

For metadata, use the format: %metadata-F_metadataFieldAssetid%

To find new keywords, v3.14 of Matrix has a dialog box that shows you the keywords for each attribute of each asset type.

(Eric) #20

Can anybody tell me what is the keyword for date start field in a calendar asset 3.12.3


I tried %details-F_start_date% I found it in another post but it doesn't work?



%details-F_end_date% also doesn't work, but the other keywords working no problem %details-F_name% %details-comments% although comments look like a WYSYWIG field, any way to make it an ordinary multirow text field.



This is a code



Inspection type: %metadata-F_8823%



Date required: %details-F_start_date% %details-F_end_date%



%metadata-F_8825%



Inspection Address:

Number: %metadata-F_8826%



Street: %metadata-F_8827%



Suburb: %metadata-F_8828%



Contact Details

Name: %details-F_name%



Phone: %metadata-F_8829%



Email: %metadata-F_8830%



Comments: %details-F_description%



%commit_button%







Everything prints except a start/end date.



Help