SlingModel not mapping the JCR properly - jcr

I have a SlingModel called TextModel.
#Model(adaptables=Resource.class, defaultInjectionStrategy= DefaultInjectionStrategy.OPTIONAL)
public class TextModel {
#Inject
private String heading;
#Inject
private String description;
public String getHeading() {
return heading;
}
public String getDescription() {
return description;
}
}
I also have a template in Sightly which renders the component:
<div data-sly-use.model="project.components.slingmodels.text.TextModel" data-sly-unwrap/>
<div>
<p>PageModel component</p>
<h1>${model.heading}</h1>
<p>Description: ${model.description}</p>
</div>
Then I embed the component in a page:
<div data-sly-resource="${# resourceType='project/components/textModel'}" data-sly-unwrap></div>
And create the initial JCR structure via JSON:
{
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "project/pages/page",
"title" : "Welcome page",
"jcr:content" : {
"myContent" : {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType" : "project/components/textModel",
"heading": "Heading",
"description": "test description"
}
}
}
All the fields are properly saved in JCR, but my Sling Model returns null as a value of both heading and description.
But, when I create the content like this:
{
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "project/pages/page",
"title" : "Welcome page",
"heading": "Heading",
"description": "test description",
"jcr:content" : {
"myContent" : {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType" : "project/components/textModel"
}
}
}
It works. The JSON is stored under jcr_root/content/hello.json in my project files and I am opening a localhost:8080/content/hello.html URL in my browser.

You should include your component with the right path, otherwise the path points to the current resource and this resource is the jcr:content of the current page.
<div data-sly-resource="${ #path='componentPath',
resourceType='project/components/textModel'}" data-sly-unwrap></div>

I suggest to use:
<div data-sly-resource="${'myContent' # resourceType='project/components/textModels'}" data-sly-unwrap></div>
or even better:
<sly data-sly-resource="${'myContent' # resourceType='project/components/textModels'}"></sly>

Related

How to access parameter from graphql query

Using DatoCMS, with VueJS and gridsome. The page query looks like
<page-query>
{
DatoCms {
_site {
globalSeo {
facebookPageUrl
siteName
titleSuffix
twitterAccount
fallbackSeo {
description
title
twitterCard
image {
url
}
}
}
}
}
}
</page-query>
Below does not work to get the values from the query
export default {
metaInfo: {
title: this.$page.DatoCms._site[0].globalSeo.fallbackSeo.title,
the query needed to be adjusted and metainfo method updated

How to add tenant / organization id to every URL and read it inside controller

I am writing an ASP.NET Core web application. In my application, the user can be a member of one or more organizations (this information is stored in the DB). I would like to give the users the possibility to select the context of organization in which the whole app should be running. This context should be passed to every controller so that I can do proper checks and return only the data from the selected org.
In other words, I would like to achieve the state when the user can access the app via:
app.mydomain.com/org1/controller/action/id and using a dropdown they should be able to switch to org2 and access app.mydomain/org2/controller/action/id
In both cases I need to be able to read the organization inside the controller. This should apply to every controller.
How should this be done? Is it possible to construct a route that would handle it and just add new parameter to every controller, e.g. orgId? Or maybe there should be a service that reads this information from the URL and can be injected into controllers? How would the route look then? I have read some tutorials about handling multiple languages in a similar way (with the culture in URL) but I am not able to translate that into my case.
For how to pass the select org id to the controller:
View:
<form action="/Orgnization/Test">
<select id="select" name="select" asp-items="#ViewBag.Sel"></select>
</form>
#section Scripts
{
<script>
$(document).ready(function () {
$("#select").change(function () {
$('form').submit();
});
})
</script>
}
Configure selectlist:
public class HomeController : Controller
{
public IActionResult Index()
{
//for easy testing,I just set it manually...
ViewBag.Sel = new List<SelectListItem>() {
new SelectListItem() { Value = "-1", Text = "--- Select ---" },
new SelectListItem() { Value = "org1", Text = "org1" },
new SelectListItem() { Value = "org2", Text = "org2" },
new SelectListItem() { Value = "org3", Text = "org3" },
};
return View();
}
}
Controller:
public class OrgnizationController : Controller
{
public IActionResult Test(string select)
{
//the select is the value you choose
//do your stuff....
return View();
}
}
If you want to add prefix to the route and get it in the controller:
Startup.cs:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{orgid?}/{controller=Home}/{action=Privacy}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Privacy}/{id?}");
});
Controller:
public class OrgnizationController : Controller
{
public IActionResult Test(string select)
{
var org = "";
if (select==null)
{
var path = Request.Path.ToString();
org = path.Split('/')[1];
}
//do your stuff....
return View();
}
}
Result:

Prismic/GraphQL and Gridsome/Vue the rendered v-html shows inside the string

So I made my site using Prismic/GraphQL and Gridsome. And I got the query working, only that when rendering the strings to the rendered string shows where only spaces would go.
Im using v-html to render the string. I even tried to format it but it seems to me like im missing something?
<p v-for="(p, index) in $page.prismicio.post.edges[0].node.content" :key="index" v-html="p.text" />
My graphQl is just the basic variant:
<page-query>
query Post ($uid: String!) {
prismicio {
post: allNewss(uid: $uid) {
edges {
node {
_meta {
id
uid
}
title
date
description
content
image
}
}
}
}
}
</page-query>
And this is part what is output in grahQL explorer:
"content": [
{
"type": "paragraph",
"text": "Lectus hendrerit curae nostra.",
"spans": []
},
...
but the actual output is this:
Lectus hendrerit curae nostra.

yii.activeform.js generate invalid validation js script

in a ok project,it should like this:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#form-signup').yiiActiveForm({
"username": {
"validate": function(attribute, value, messages) {
yii.validation.required(value, messages, {
"message": "Username\u4e0d\u80fd\u4e3a\u7a7a\u3002"
});
yii.validation.string(value, messages, {
"message": "Username\u5fc5\u987b\u662f\u4e00\u6761\u5b57\u7b26\u4e32\u3002",
"min": 2,
"tooShort": "Username\u5e94\u8be5\u5305\u542b\u81f3\u5c112\u4e2a\u5b57\u7b26\u3002",
"max": 255,
"tooLong": "Username\u53ea\u80fd\u5305\u542b\u81f3\u591a255\u4e2a\u5b57\u7b26\u3002",
"skipOnEmpty": 1
});
},
"id": "signupform-username",
"name": "username",
"validateOnChange": true,
"validateOnType": false,
"validationDelay": 200,
"container": ".field-signupform-username",
"input": "#signupform-username",
"error": ".help-block"
},
</script>
but in my project,when i enter email or username that is invalid,no hints or error shows! when i check the code generated by yii,i see null args of yiiActiveForm
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('#form-signup').yiiActiveForm([], []);
});
</script>
who can tell me why? IS there anything wrong with the vendor folder?
This might be because your model class is not set up to be in the right scenario, causing validation to be skipped. Please check the documentation on this topic:
http://www.yiiframework.com/doc-2.0/guide-structure-models.html#scenarios
If you want certain rules to be applied on the current view, you need to set the scenario when you create the instance of your model :
// scenario is set as a property
$model = new User;
$model->scenario = 'login';
// scenario is set through configuration
$model = new User(['scenario' => 'login']);
In your model, the case above User, you would have this function:
namespace app\models;
use yii\db\ActiveRecord;
class User extends ActiveRecord
{
public function scenarios()
{
return [
'login' => ['username', 'password'],
//more scenarios would follow here...
];
}
}

how to disable a DataTables/TableTools button

I'm using DataTable 1.10 and TableTools 2.2.1.
Given the following snipped I would like to disable/enable the edit-button.
var myTable = $("#myTable ").DataTable({
tableTools : {
"aButtons" : [ {
"sExtends" : "text",
"sButtonText" : "Edit",
"fnClick" : function(nButton, oConfig, oFlash) {
/* some stuff */
}
}]
}
})
Is there a possibility to do this at runtime?
Thanks a lot
This was a good question! Seems that the fnClick in
dataTable.tabletools().fnSettings().buttonSet[id].fnClick
only is a reference to the event stored elsewhere, not accessible (changing fnClick on the API has no effect). However, you can use the predefined class DTTT_disabled and check for that in your fnClick-handler :
var dataTable = $("#example").DataTable({
sDom: 'TC',
oTableTools : {
aButtons : [{
sExtends : "text",
sButtonText : "Edit",
fnClick : function(nButton, oConfig, oFlash) {
if ($(nButton).hasClass('DTTT_disabled')) return;
alert('edit button clicked');
}
}]
}
});
example with a checkbox enabling or disabling the button :
$("#enable").click(function() {
if ($(this).is(':checked')) {
$('.DTTT_button_text').removeClass('DTTT_disabled');
} else {
$('.DTTT_button_text').addClass('DTTT_disabled');
}
});
see demo -> http://jsfiddle.net/ev2N2/