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

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.

Related

Only use certain fields from json in nuxt app

By using asyncdata and axios I am grabbing a json object from a database called Knack.
Working great however the whole response is huge and in some pages I’m only using say 10 fields from sometimes 50+.
So I’m grabbing records from knack and then using v-for to loop through and output 10 of the 50+ fields from say 200 records.
By looking in console I can see the whole json object.
Is there a way to get the json object as I am and rather than returning it all, loop through and create a smaller object with the fields I need and returning that to then do a v-for in my template? Basically resulting in only the required fields being visible in console and to the public?
Thanks so much if someone can help with a code sample.
You can do it this way.
Sample data model
data() {
return {
// Required fields to filter data
requiredFields: ["name", "phone", "email"],
fields: {
name: "Wade Mckenzie",
phone: "1-278-483-8300",
email: "ac.mattis.velit#magnisdisparturient.net",
address: "971-2324 Id, Av.",
list: 7,
country: "Australia",
postalZip: "422625",
region: "Campania",
text: "malesuada augue ut lacus. Nulla tincidunt, neque vitae semper egestas,",
numberrange: 5,
currency: "$9.91",
alphanumeric: "TXQ00DEL5RP",
},
};
}
Computed prop
computed: {
filteredData() {
// Create an empty object
let requiredData = {};
// Loop over the entries
for (const [key, value] of Object.entries(this.data)) {
// Check if the property is a required field
if (this.requiredFields.includes(key)) {
requiredData[key] = value;
}
}
return requiredData;
},
}
Template
<div v-for="(value, name) in filteredData" :key="name">
{{ name }}: {{ value }}
</div>
You can check out the working example.
You can do that in a computed property.
computed(){
getCustomObject(){
let customObject = {};
for(let property in yourJsonObject){
if(property == 'the field you want'){
customObject[`${property}`] = yourJsonObject[property]
}
}
return customObject;
}
}
In this way you can get specific fields that you want. But you will have to specify them by placing || in the if statement.
In template you can simply render it like this: -
<div v-for="(item,key,index) in getCustomObject" :key="index">
//Render data...
</div>
Or if you just want a specific number of fields like 10 fields from 50, you can do it like this: -
computed(){
getCustomObject(){
let customObject = {};
Object.entries(yourJsonObject).forEach(([key,value],index) => {
if(index<10){
customObject[`${key}`] = value;
}
})
return customObject;
}
}

Vue search while typing

I have search field and I wish to have the results in real-time,
I have no issue with returning data or showing data but I need a way to send input value to back-end while user is typing it.
Code
HTML
<el-input placeholder="Type something" v-model="search">
<i slot="prefix" class="el-input__icon el-icon-search"></i>
</el-input>
Script
data() {
return {
search: '' // getting data of input field
}
},
I've tried to compute it but it didn't return data
computed : {
searchSubmit: {
get: function () {
console.log(this.search);
}
}
},
Any idea?
For side effects as calling backend you can use Watchers.
watch: {
search(value) {
yourApiCall(value)
}
}

Vue interpolation does not work in the text obtained by API

Get text by API, in text have a entities in {{}}, like a:
Some text {{rules}} other text
Have in data values:
rules: "some text"
but this values dont interpolated, displays:
Some text {{rules}} other text
thanks for answer
Considering the comment above:
I have: template: {{someText}} have data: data() { return {
someText: "some text {{ myValue }} some text", myValue: "300", } },
How display data from myValue in template?
You don't do interpolation like that in Vue. Double braces ({{}}) are for the template part, not the scripts.
Look at this snippet:
new Vue({
el: "#app",
data() {
return {
someText: "some text {{ myValue }} some text",
myValue: "300"
}
},
computed: {
parsedSomeText() {
let ret = ''
if (/(\w+)(?= }})/g.test(this.someText)) {
let key = String(this.someText).match(/(\w+)(?= }})/g)[0]
if (Object.keys(this.$data).includes(key)) {
ret = this.someText.replace(/{{ (\w+) }}/g, this.$data[key])
}
}
return ret
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
myValue: {{myValue}}<br /> someText: {{someText}}<br />parsedSomeText: {{parsedSomeText}}
</div>
I created a small parser in parsedSomeText() computed property, that very crudely replaces the double braces in the someText data property and returns the modified string, so to get it working in the data property.
I advise you to look over the data you receive, and think of another solution - or utilize some rock-solid parsing technique to use it in production.
If you cannot avoid getting data in such a way, then you should look into dynamic (run-time) compilation (like: https://alligator.io/vuejs/v-runtime-template/) and render functions (https://v2.vuejs.org/v2/guide/render-function.html). With these, your Vue app becomes more versatile but more complex.

SlingModel not mapping the JCR properly

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>

Error while using Paginated Lucene Search in cloudant

I'm currently having an issue in lucene cloudant implementation with pagination.
{"error":"scala.Symbol cannot be cast to org.apache.lucene.util.BytesRef","reason":null}
The URL that I am trying to access :
/_design/contact/search/name?q=name%3Asa%2A+OR+default%3Asa%2A&limit=10&bookmark=g1AAAAEPeJzLYWBgYMlgTmGQTUlKzi9KdUhJMtMrzsnMS9dLzskvTUnMK9HLSy3JASpjSmRIsv__38WmJPCwJJXmpOTGJeFqtscl-4kByCZVI9hQDyaAaa4DMhjAZIMDUAKaMZ-VEMyiXUFxJADEEPQXOKTlQUASZpV2Q&stale=ok&sort="name<string>"
What I found :
If I remove the bookmark ( means 1st page ) it works fine.
Or, If I remove the sort, it works fine.
below is the index that I created for this view :
"indexes": {
"name": {
"index": "function (doc)
{
if (doc.Type == 'contact')
{
index("default", doc._id);
index("name",doc.Name,{"store": "yes"});
if(doc.Profile) {index("profile", doc.Profile, {"store": "no"});}
if (doc.Aliases)
{
if (Array.isArray(doc.Aliases))
{
doc.Aliases.forEach(function (alias){
index("alias", alias, {"store":"yes"})
})
}
else
{
index("alias", doc.Alias_Name, {"store":"yes"})
}
}
}
}"
}
}
We have deployed a fix for this issue and you should no longer be experiencing this problem. Please confirm that that is the case. Thanks!