Changes for page XWiki JavaScript API

Last modified by Simon Urli on 2022/09/14

<
From version < 28.1 >
edited by Guillaume Delhumeau
on 2015/05/28
To version < 31.1 >
edited by Marius Dumitru Florea
on 2015/07/16
>
Change comment: There is no comment for this version

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.gdelhumeau
1 +XWiki.mflorea
Content
... ... @@ -201,7 +201,7 @@
201 201  {{code language="javascript"}}
202 202  require(['xwiki-meta'], function (xm) {
203 203   xm.document // get the current document (eg: Main.WebHome)
204 - xm.wiki // get the current wiki (eg: xwiki)
204 + xm.wiki // get the current wiki (eg: xwiki)
205 205   xm.space // get the current space (eg: Main)
206 206   xm.page // get the current page name (eg: WebHome)
207 207   xm.version // get the current document version (eg: 1.1)
... ... @@ -229,5 +229,51 @@
229 229   var version = xm ? xm.version : $('meta[name="version"]').attr('content');
230 230   var restURL = xm ? xm.restURL : $('meta[name="restURL"]').attr('content');
231 231   var form_token = xm ? xm.form_token : $('meta[name="form_token"]').attr('content');
232 -}
232 +});
233 233  {{/code}}
234 +
235 += Work with Entity References =
236 +
237 +Starting with XWiki 4.2M1 you can resolve and serialize Entity References on the client side easily:
238 +
239 +{{code language="js"}}
240 +var documentReference = XWiki.Model.resolve('wiki:Space.Page', XWiki.EntityType.DOCUMENT);
241 +var attachmentReference = new XWiki.AttachmentReference('logo.png', documentReference);
242 +XWiki.Model.serialize(attachmentReference);
243 +{{/code}}
244 +
245 +You can check the full API [[here>>https://github.com/xwiki/xwiki-platform/blob/master/xwiki-platform-core/xwiki-platform-web/src/main/webapp/resources/uicomponents/model/entityReference.js]].
246 +
247 +Starting with XWiki 7.2M1 the {{XWiki.Model}} JavaScript API is supporting nested spaces:
248 +
249 +{{code language="js"}}
250 +var documentReference = XWiki.Model.resolve('wiki:Path.To.My.Page', XWiki.EntityType.DOCUMENT);
251 +documentReference.getReversedReferenceChain().map(function(entityReference) {
252 + return entityReference.type + ': ' + entityReference.name
253 +}).join()
254 +// Will produce:
255 +// 0: wiki,1: Path,1: To,1: My,2: Page
256 +{{/code}}
257 +
258 +Starting with 7.2M1 you can also pass a 'provider' as the third argument to {{XWiki.Model.resolve}}. The provider is used to fill missing references, and it is either a function that gets the entity type, an array of entity names or an entity reference.
259 +
260 +{{code language="js"}}
261 +var documentReference = XWiki.Model.resolve('Page', XWiki.EntityType.DOCUMENT, function(type) {
262 + switch(type) {
263 + case: XWiki.EntityType.WIKI:
264 + return 'wiki';
265 + case: XWiki.EntityType.SPACE:
266 + return 'Space';
267 + default:
268 + return null;
269 + }
270 +});
271 +// Produces wiki:Space.Page
272 +
273 +var documentReference = XWiki.Model.resolve('Page', XWiki.EntityType.DOCUMENT, ['wiki', 'Space']);
274 +// Same output
275 +
276 +var spaceReference = new XWiki.SpaceReference('wiki', 'Space');
277 +var documentReference = XWiki.Model.resolve('Page', XWiki.EntityType.DOCUMENT, spaceReference);
278 +// Same output
279 +{{/code}}

Get Connected