2sxc automatically serializes the modules data - IF you allow it to.
To experience this feature:
- go to the settings of a Template/View
note: in 2sxc 5 & 6 this was a module-setting
- enable the check-box
- tell the system which streams you want to publish (usually "Default" and maybe some more)
- then start writing your JavaScript
Note: the automatic system creates JSON but not JSON-P. This is a security feature.
Accessing the Data from JavaScript
Using this data in JavaScript is provided using the $2sxc(...).data object. Your code would usually look a bit like this:
// Load the data from the 2sxc module and define a callback
$2sxc(moduleId).data.on("load", function (source, data) {
// your custom code here
}).load();
Note that there are a few things that are a bit unique about this, which I'll explain a bit more.
Deprecated Features
Enabling this JSON Publication in Code
This is almost never necessary, but there may be situations where you want to selectively enable/disable the JSON publication (new in v.6, unimportant since 8.0). The code would look as follows:
@using System.Data
@using ToSic.Eav.DataSources
@functions
{
// Official place to provide data preparation. Is automatically called by 2sxc
public override void CustomizeData()
{
// enable publishing
Data.Publish.Enabled = true;
Data.Publish.Streams = "Default,UIResources";
}
}
History
- Feature added in 5.2.0
- Added ability to specific which streams in 6.0.6 (before you couldn't edit that field)
- Added ability to override these settings in the razor backend in 6.0.6
- Modified URL-Schema in 8.0 to improve performance and not run requests through DNN-pages. The JS access stayed the same, through the $2sxc object.