Use javascript variable instead json file - javascript

here I have a declaration:
<div id="timeline-embed"></div>
<script type="text/javascript">
var timeline_config = {
width: "100%",
height: "100%",
debug: true,
rows: 2,
source: 'Timeline/example_json.json'
}
</script>
<script type="text/javascript" src="Timeline/compiled/js/storyjs-embed.js"></script>
so now I want to instead source: 'Timeline/example_json.json' to use
something like:
source: '{
"timeline":
{
"headline":"Sh*t People Say",
"type":"default",
"text":"People say stuff",
"startDate":"10/4/2011 15:02:00",
"date": [
{
"startDate":"10/4/2011 15:10:00",
"endDate":"10/4/2011 15:55:00",
"headline":"prvo",
"text":"<p>dddddddddddddddd dd</p>",
"asset":
{
"caption":"yessss"
}
},
{
"startDate":"10/4/2011 17:02:00",
"endDate":"10/4/2011 18:02:00",
"headline":"drugo da da",
"text":"<p>In true political fashion, his character rattles off common jargon heard from people running for office.</p>",
"asset":
{
"media":"http://youtu.be/u4XpeU9erbg",
"credit":"",
"caption":""
}
}
]
}
}'
but doesn work. I really dont know what is probem exactly... Please help.
So this is verite Timeline plugin and this plugin need JSON as source, but is there any way to change source with Javascript variable?

The way you have this written, the value of "timeline_config.source" is a String, not a "usable" Javascript object.
You may need to convert the string to an object using JSON.parse(). So add this line of code as the last line of your first < script > block.
timeline_config.source = JSON.parse(timeline_config.source)
Alternatively, as pointed out in the comments, you can simply remove the single quotes around the string, and it will natively by a JS Object.

Try removing the ' string delimiter, so it says
source: {
"timeline": { ... }
},
This way it is a JS object and not a string.

Related

How to display dynamic variable in HTML

I'm wondering how to display a number in HTML that was created dynamically later on in the code. For instance I initialize the value reportDataLength in data(), and I tested that it was returning the right value by doing a console.log(), but now I want to display this value in HTML?
name: 'notification-tray',
data() {
return {
headers: [
{
text: 'Notifications',
value: 'Body',
width: '380px',
},
],
reportData: [],
reportDataLength: 0,
};
},
async created() {
await this.getReportDataLength();
},
methods: {
async getReportDataLength() {
... this.reportDataLength = this.reportData.length;
console.log(this.reportDataLength);
},
},
};
But obviously when I do something like this,
<span class="d-none">Notification Button</span>
<span class="badge">this.reportDataLength</span>
it doesn't work correctly. The other solutions I saw for similar problems used JQuery, and I feel like there's a simple way to reference this, but I can't seem to find it out.
okay so if you are using pure Javascript , u can use this in your javascript function :
Document.getElementsByClassName('badge').innerHtml=this.reportDataLength ;
otherwise if you are using vue js you can try something like this in your html file :
<span class="badge">{{reportDataLength}}</span>
You can do document.getElementById("ID-HERE").innerHTML = "new stuff", but be warned that setting innerHTML is dangerous.

Fill alpaca properties with a variable

I need to assemble a form dynamically using Alpaca Forms. I have the code below:
$("#form").alpaca({
"schema": {
"type":"object",
"properties": {
"email": {
"type":"string",
"title":"E-mail",
"required":true
},
"mensagem": {
"type":"string",
"title":"Mensagem",
"required":true
}
}
}
});
And it needs to be filled in using variables for it to be generic, because the amount of fields that the form will have will always vary. Ex: sometimes in addition to the "email", "mensagem" properties, I will have the "assunto", "remetente", "destinatario" properties
it would need to look something like this:
$("#form").alpaca({
"schema" : { schemaVariable }
});
What you want to achieve is possible, you just need to remove the braces from your variable and use proper JSON object declaration.
"schema" : schemaVariable
here's a working fiddle for this.
Tell me if you need something else.

Elasticsearch setting up stemming and analyzer questions

I'm using ES with my node server via the package "elasticsearch": "12.1.3".
I do bulk inserts of my documents. Excerpt:
var body = [];
_.each(rows, function(doc) {
body.push({
update: {
_index: 'mytest',
_type: 'mydoc',
_id: doc.id,
_retry_on_conflict: 3
}
});
body.push({
doc: doc,
doc_as_upsert: true
});
});
client.bulk({
body: body
}, ...
On demand, to individually update documents, I have this in place:
client.index({
index: 'mytest',
type: 'mydoc',
id: doc.id,
body: doc.body
}, ...);
Everything works as expected so far. Now I'm trying to add basic 'light_english' stemming.
Looking at the Docs here
and for the JS package here
I want certain fields in my document to be "fuzzy" matched, therefore I think stemming is the way to go?
It is not clear to me how I would set this up.
Assuming I use the example settings from the link above, would this be the right way to do it:
client.cluster.putSettings({
"settings": {
"analysis": {
"filter": {
"no_stem": {
"type": "keyword_marker",
"keywords": [ "skies" ]
}
},
"analyzer": {
"my_english": {
"tokenizer": "standard",
"filter": [
"lowercase",
"no_stem",
"porter_stem"
]
}
}
}
}
});
And would this then work permanently for my two code examples above, if applied once?
Bonus question: What would be a good default analyzer plugin (or settings) I can use? My main goal is that searches for example: "Günther" would also match "gunther" and vice versa.
Might it be better to do this manually before inserting/updating documents, so that strings are lower-cased, diacritics removed etc.?

Iterate through poorly formated object

I have a really bad-formated javascript object:
commits = {
commit: {
name: 'First commit'
},
commit: {
name: 'Second commit'
}
}
As you can see, each sub-object of commits object is called commit so it practically precludes an option to use for ... in ... or any other javascript loop (well, that's what i think but i'm a really poor JS programmer so i'm probably wrong). So, the question is, how can i iterate through that object?
Please have in mind that i can't use jQuery here and i can't rewrite that object
edit: that object is parsed from the following json:
{
"commits": {
"commit": {
"name": "First commit"
},
"commit": {
"name": "Second commit"
},
}
}
Considering the JSON posted in the github link you provided, there's not a lot you have to do.
The JSON string seems to be valid, except that it's missing a } at the end of the string. With that fixed, it parses just fine:
JSON.parse('{\n \"accountURL\": \"https://domain.com\",\n \"newCommitsCount\": \"1\",\n \"pushURL\":\"https://domain.com/project/64249/git/source/compare/revisions/0b6438955f2a5a7981fd25cfa5b48fe3fb4c888d,7771e638d1356a14d1dc46f3f5cfaab858370a5e\",\n \"unsubscribeURL\": \"https://domain.com:443/unsubscribe?token=receiverToken&type=COMMITS&projectId=64249\",\n \"invokerEmail\": \"email#email.com\",\n \"projectURL\": \"https://domain.com/project/64249\",\n \"projectId\": \"64249\",\n \"afterPushRevision\": \"7771e638d1356a14d1dc46f3f5cfaab858370a5e\",\n \"invokerId\": \"38074\",\n \"pushDate\": \"2014-02-11T15:26:36+0000\",\n \"beforePushRevision\": \"0b6438955f2a5a7981fd25cfa5b48fe3fb4c888d\",\n \"repositoryURL\": \"git_url\",\n \"subdomain\": \"subdomain\",\n \"domain\": \"domain\",\n \"branch\": \"develop\",\n \"invokerProfileURL\": \"url\",\n \"commitsCount\": \"1\",\n \"invokerSmallAvatarURL\": \"xx\",\n \"projectName\": \"NAME\",\n \"invoker\": \"Invoker Name.\",\n \"commits\": {\"commit\": {\n \"revision\": \"7771e638d1356a14d1dc46f3f5cfaab858370a5e\",\n \"commitMessage\": \"quickfix\",\n \"committerId\": \"38074\",\n \"committerEmail\": \"email\",\n \"committerName\": \"Name.\",\n \"commitDate\": \"2014-02-11T15:26:27+0000\",\n \"commitURL\": \"https://domain.com/project/64249/git/source/commit/develop/7771e638d1356a14d1dc46f3f5cfaab858370a5e\" }}}')

How do I access JSON elements through javascript?

The JSON file I have is following:
{
"root": {
"Quizsize": null,
"{urn:abc.com/xmlns/mona/page}page": [
{
"#xid": "page1623",
"#title": "Quiz Landing Page",
"{urn:abc.com/xmlns/mona/page}comment": null,
"{urn:abc.com/xmlns/mona/page}skills": null,
"{urn:abc.com/xmlns/mona/page}info": {
"{urn:abc.com/xmlns/mona/common}property": {
"#name": "quiz_landing",
"#value": "true"
}
}
}
]
}
}
}
I am loading this JSON file using :
var jsondata = eval("("+chapterRequest.responseText+")") ;
Root = jsondata.root
Now I want to access #xid which is "page1623 and #name which is "quiz_landing". I dont know how to do this,Please help.
Thanks
JSON.parse(x) is better than eval(x), for one. It is not supported natively by some browsers though. If you want to access #xid, "urn:abc.com/xmlns/mona/page}page" points to an array whose first element is an object.
So use Root["{urn:abc.com/xmlns/mona/page}page"][0]["#xid"]. Or mix bracket and dot notation. Your choice, really.
When the key isn't a valid identifier, you use object['key'] instead of object.key, so jsondata.root['{urn:abc.com/xmlns/mona/page}page'][0]['#xid'].

Categories

Resources