I’m making a live editor that would allow me to edit and add objects to a document, and then use the list in other documents. I would like to make these documents more like database tables with rows in them that will be XObjects attached to the document.
## Create an object
#set($obj = $doc.newObject("XWiki.SomeClass"))
$obj.set("field1",$value1)
$obj.set("field2",$value2)
## Save the object in the page
$doc.save()
As shown in the Scripting API Guide, but I don’t understand how to do this when the button is pressed.
A javascript routine is needed which is activated by an event (e.g. onclick=, onchange= …)
This routine should generate an ajax call which invokes a wiki page which contains script code (velocity, groovy …). This script code changes the object.
This javascript code contains stuff to generate the ajax request like:
var Url = new XWiki.Document('WebHome','localtools.updateObject').getURL('get');
var UrlParams = { 'xpage': 'plain', // this is really needed !
'outputSyntax': 'plain', // this is really needed !
'parameter1' :'anything1',
'parameter2' :'anything2',
'parameter3' : JSON.stringify(A_list_of_keys),
'parameter4' : JSON.stringify(A_list_of_values)
};
require(["jquery"],function($) {
$.get( Url, UrlParams, function( data ) {
}, "json" );
});
The URL-Parameter “xpage=plain” and “outputSyntax=plain” are needed to avoid any additional HTML output from the wiki, when doing the ajax request.
The additional parameters (1…4) are examples to give the information to your called document.
Second:
A document with velocity or javascript code:
In this example the document “localtools.updateObject.WebHome” is called.
It can refer the parameter values with (velocity) “$request.parameter1”, “$request.parameter2” … or within groovy without “$”.
This code should do the wanted actions to create objects.
Typically results may be returned to the javascript with jsontool.serialize(SOME_RESULT)
It’ll do for the start. One thing I found when I was trying to figure out how to do this properly is that there is already rest interface to delete, edit and add objects. And also dataeditors.js that takes advantage of it. But I decided to write my own page still for now, because that dataeditors.js has a 800-lines long function which I couldn’t understand.