I have the following objects in some dedicated json file:
{
"obj1": {
"firstField": "a",
"secondField": "b"
}
},
{
"obj2": {
"firstField": "c",
"secondField": "d"
}
}
I would like to read this json from my grunt file, inside initConfig.
I use:
grunt.initConfig({
configuration: grunt.file.readJSON('configuration.json')
)
}
Now I want to get Obj1 or Obj2 - and give them a name, so I could extract firstField generically.
Something like:
obj: '<%= configuration.obj1 %>
and then use:
'<%= obj.firstField %>
But this one is not working. How can I do it?
Related
I'm doing some work to edit a configuration file. When I try, things are not quite working right.
Here's my code
config.module.rules.unshift( {
test: "/ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+\.svg$/",
use: [ 'raw-loader' ]
}
);
fs.writeFileSync("C:\\temp\\config.txt", JSON.stringify(config) );
This outputs a file that contains the following text...
{
"test": "/ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+.svg$/",
"use": [
"raw-loader"
]
},
however the value of "test" is incorrect. I'd like to have it set to
{
test: /ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+\.svg$/,
use: [ 'raw-loader' ]
}
Notice how there are no quotes around the regular expression string.
If I try to use
config.module.rules.unshift( {
test: /ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+\.svg$/,
use: [ 'raw-loader' ]
}
);
fs.writeFileSync("C:\\temp\\config.txt", JSON.stringify(config) );
Here's what I get instead
{
"test": {},
"use": [ "raw-loader" ]
},
I need to get the value of test to be
/ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+\.svg$/
I'm barking up the wrong tree. As people have pointed out, the JSON Serialiser is changing the output to {}, instead of the value I was expecting. When I look at the object itself. It's set properly.
I have an object like this
{
data1: 1,
data2: 2,
file1: File,
file2: File,
file3: File,
objects: [
{name: "test1", info: "test", related: [1,2,3], files: [File, File, File]},
{name: "test2", info: "test", related: [1,2,3], files: [File, File, File]},
{name: "test3", info: "test", related: [1,2,3], files: [File, File, File]}
]
}
I need to somehow send it to the backend with data and files (ideally keeping the object structure, but it will be fine if the files are sent in formdata alongside main object and I get them by keys that I will store in objects instead of the files themselves)
I have some structure that I want to render to my JADE page, so I decided to make JSON-like object to render some kind of data (variables, text, js objects), this JSON object looks like :
var dataSet1 = {
meta: {
"name": "Some text",
"minimum": mini_2,
"maximum": maxi_2,
"currentValue": last_data_2
},
data: {
"values": dataTwo,
"corridor": {
"x1": xc,
"x2": yc2,
"yw": yw2
}
}
};
My render line:
res.render('index', {
data_to_draw: JSON.stringify(dataSet1)
});
Then I`m using this rendered data on my JADE:
displayGraphExampleOne("#graph",
!{data_to_draw.data.values},
!{data_to_draw.meta.currentValue},
!{data_to_draw.meta.minimum},
!{data_to_draw.meta.maximum},
!{data_to_draw.meta.name},
!{data_to_draw.data.corridor.x1},
!{data_to_draw.data.corridor.x2},
!{data_to_draw.data.corridor.yw2});
Cannot read property 'values' of undefined
Im getting such type of error.
Im new with JS , so Im trying to decide what i`m doing wrong. If I will pass data not in js object - it works well, but i need such type of passing data.
thanx
Don't JSON.stringify the object, instead pass the object itself, otherwise you are trying to access the properties of a string, which obviously don't exist.
Just need to format code like this:
var dataSet1= [
{
"meta": {
"name": "Veocity variance",
"minimum": mini_1,
"maximum": maxi_1,
"currentValue": last_data_1
},
"data": {
"values": dataOne,
"corridor": {
"x1": xc,
"x2": yc1,
"yw": yw1
}
}
}
];
And use such call:
displayGraphExampleOne("#graph",
!{first_set}[0][0].data.values,
!{first_set}[0][0].meta.currentValue,
!{first_set}[0][0].meta.minimum,
!{first_set}[0][0].meta.maximum,
!{first_set}[0][0].meta.name,
!{first_set}[0][0].data.corridor.x1,
!{first_set}[0][0].data.corridor.x2,
!{first_set}[0][0].data.corridor.yw);
But not forget to render:
res.render('index', {
first_set: JSON.stringify([dataSet1, dataSet2, dataSet3]),
second_set: JSON.stringify([dataSet1, dataSet2, dataSet3]),
third_set: JSON.stringify([dataSet1, dataSet2, dataSet3])
});
I'm following the official documentation page about the topic but I cannot configure it to ignore .txt files.
I have a all.profile.js on the root of my project:
var profile = (function(){
return {
basePath: "./",
releaseDir: "../web",
action: "release",
layerOptimize: "closure",
optimize: "closure",
cssOptimize: "comments",
mini: true,
stripConsole: "all",
packages: [
{ name: "myapp", location: "myapp" }
]
};
})();
And this is the package.json inside the folder myapp:
{
"dojoBuild": "myapp.profile.js",
"name": "my-app",
"description": "This is My App",
"version": "1.0",
"main": "src"
}
And this is the myapp.profile.js also inside the folder myapp:
var profile = (function(){
return {
// don't need to do anything on the 'test' folder so don't define it on the trees.
trees: [
["libs","libs",/txt$/], // ignore .txt files -> DOESN'T WORK!
["src","src",/txt$/] // ignore .txt files -> DOESN'T WORK!
],
resourceTags: {
test: function(filename, mid){
console.log("[test] filename: ",filename);
return filename.indexOf("test/") !== -1;
},
copyOnly: function(filename, mid){
//console.log("[copyOnly] mid: ",mid);
return false;
},
miniExclude: function (filename, mid) {
console.log("[miniExclude] filename: ",filename);
return mid in {
'myapp/myapp.profile': 1,
'myapp/package.json': 2
} || mid.indexOf(".txt") !== -1; // .txt are not ignored so exclude them...
},
amd: function(filename, mid) {
//console.log("[amd] mid: ",mid);
// myapp is not AMD but this will 'convert' it
return false;
}
}
};
})();
Finally, this is the folder structure:
web_dev/
-- myapp/
---- libs/
---- src/
---- test/
---- myapp.profile.js
---- package.json
-- all.profile.js
The build tool runs fine, it reads and process all files but the .txt files are still on the release dir.
Please let me know if you spot any mistakes on my logic or how I'm configuring the build system. I'm using Dojo 1.9.1.
Thanks in advance.
I'm not sure what is wrong with my initial scenario but here are the changes that I made to have the desired result:
Move the trees declaration from the myapp.profile.js to
all.profile.js inside the 'myapp' package definition.
Instead of specifying the root of the trees, check
everything and exclude accordingly: [".", ".",
/(\/\.)|(~$)|(test|txt)/]
The final all.profile.js:
var profile = {
basePath: "./",
releaseDir: "../web",
releaseName: "built",
action: "release",
layerOptimize: "closure",
optimize: "closure",
cssOptimize: "comments",
mini: true,
stripConsole: "all",
packages: [
{
name: "myapp",
location: "myapp",
trees: [
// don't bother with .hidden, tests and txt.
[".", ".", /(\/\.)|(~$)|(test|txt)/]
]
}
]
};
If anyone can pin point exactly what I was doing wrong, please share.
Dust js web gives us some examples. One of them is recursion.
When I change the 'name' of the json in the 3rd section and the 'name' in 1st section to other string like 'node', the output is wrong! What am I doing wrong?
Code:
{node}{~n}{#kids}{>recursion:./}{/kids}
{
"node": "1",
"kids": [{
"node": "1.1",
"kids": [{
"node": "1.1.1"
}]
}]
}
It doesn't work because "recursion" is a template loaded in the dust context.
In dust you can load other templates by name: So when he writes this:
{>recursion:./}
He is loading a template called recursion. you can find it in the examples files.
this is the recursion template:
{
name: "recursion",
source: "{name}{~n}{#kids}{>recursion:./}{/kids}",
context: {
name: '1',
kids: [
{
name: '1.1',
kids: [
{name: '1.1.1'}
]
}
]
},
expected: "1\n1.1\n1.1.1\n"
}
You should create another template with the node fields, load it in the dust context and then just use it.