0

Im trying to embed my 2sxc App into a Theme layout page. The App view html does render

<%-- This namespace provides this.GetScopedService<T>() --%>
<%@ Import Namespace="ToSic.Sxc.Dnn" %>
<%-- This namespace provides all the common 2sxc services --%>
<%@ Import Namespace="ToSic.Sxc.Services" %>

<%= this.GetScopedService<IRenderService>().Module(1041,3421) %>

, but its Javascript crashes.

The App uses its own API for searching.

1. // get the sxc-controller for this module
2. var sxc = $2sxc(3421);
3. // now get the data in the promise
4. sxc.webApi.get('app/auto/api/Forms/SearchForm')
5.   .then(data => {
6.     console.log(data)
7.   });

originally line 2 crashed saying $2sxc is not recognised. we resolved that by adding this script reference to our layout page

<script src="/desktopmodules/tosic_sexycontent/js/2sxc.api.min.js" type="text/javascript"></script>

And now it crashes on line 4 when trying to use sxc.webApi.get

Uncaught Can't find page - something went wrong, pls contact 2sxc.org

It seems I need to include another JS script. I tried to also include

<script src="/desktopmodules/tosic_sexycontent/dist/inpage/inpage.min.js" type="text/javascript"></script>

but that made it worse

Dieter V
  • 11
  • 4
  • resolved by adding this var page = GetService(); page.Activate("2sxc.JsCore"); Now need to resolve the pageID, moduleID mismatch – Dieter V Jun 29 '22 at 13:32

2 Answers2

1

I resolved this by adding this

var page = GetService<ToSic.Sxc.Services.IPageService>();

page.Activate("2sxc.JsCore");

Dieter V
  • 11
  • 4
  • Two things, 1) don't forget to mark this as answered. 2) if you have upgraded to v14+ already, you can skip the `var page...` and just do `Kit.Page.Activate();` now. Lots of good examples of this any of the recently updated 2sxc Apps on GitHub. – Jeremy Farrance Jul 09 '22 at 09:01
0

Update: to use the JS better in the theme, I've just pushed an update to 14.07.04.

Basically you can force a different context than the automatic one, using

var sxc = $2sxc({ pageId: 27, moduleId: 42, zoneId: 3, appId: 8});
sxc.webApi.fetchJson(...).then(...);

this uses the context identifier https://docs.2sxc.org/api/js/ContextIdentifier.html#Api_Js_SxcJs_ContextIdentifier which has existed for a few versions now, but there was a bug that was fixed in 14.07.04

See also https://docs.2sxc.org/api/js/SxcGlobal.html#Api_Js_SxcJs_SxcGlobal_get_3

iJungleBoy
  • 5,325
  • 1
  • 9
  • 21