How to read values from properties file in ExtJs - javascript

I want to read values from config.properties and use these values in my ExtJs ComboBox. I do not want to read from JSON file.
Where should I place the config.properties file? Should I place it in the webcontent directly? I do not want to hard code the path/ or at least reduce it.
And how can I access property values through JavaScript.?
Thanks

This is not ExtJS specific but in general rather applicable to javascript as a whole.
Best bet would be to either include it in a global file that gets included in your app and is namespaced appropriately to your app so as to minimise chance of collisions with any other potential variables
MyApp = {
config:{
property1:'something',
property2:'something2'
}
}
// Then you can access this anywhere you like
myProperty1 = MyApp.config.property1; // assigns the string "something" to variable myProperty1

You can load the properties file into a data store, this way you can have the key-value pairs in the your properties file mapped to a simple model with fields
key and value.
And to enable your proxy to load data from the properties file and be converted into the corresponding model you'll have to create a reader that'll read the data from the properties file and convert to the desired record.
This custom package for i18n will give you some idea how to do that.

Related

Type for JSON field on Type-GraphQL

I'm defining a custom input field on type-graphql of type JSON. We're using Prisma as well. I tried with Prisma.JsonValue, Prisma.JsonObject and JSON but I get this errors. any suggestion will be welcome
You can't directly use Prisma-generated models as type definitions in TypeGraphQL. You will need to create custom classes as shown in the TypeGraphQL docs (with #ObjectType(), #InputType() etc decorators).
There is a third party library for generating TypeGraphQL types from Prisma called typegraphql-prisma which you could consider. However, in my opinion it's easier to define the classes manually.
Furthermore, I'm not sure what you mean by JsonValue. If you need to pass arbitrary JSON data, perhaps you could stringify your JSON object and pass it as a String?

Search and update value in json file in node js

I'm trying to create a key-value paired datastore in nodejs in which I'm trying to use a json file as a database. Now I'm trying to read a particular value of key and update it or delete it.
For eg.(dummy data)
{
name: 'my name',
no:12345,
course : {
name: 'cname',
id: 'cid'
}
}
Now I want to change it as
{
name: 'my name',
no:12345,
course : {
name: 'cname1',
id: 'cid1'
},
fees: {
primaryFee: 1000,
donation: 100,
}
}
Or even delete the course key with it's value.
One way I thought of to achieve this is read the entire file and store it in a variable(json parsed). And update the value in that variable and write the whole data to the file.
But this is not efficient as it reads and writes the whole data every time of an update.
So is there any efficient approach for this to update or delete a particular key in the file itself???
Yes, even in a high-level programming language like JS, it is possible to update parts of a file, though it is usually more common in low-level programming languages like e.g. C
For node.js, check-out the official documentation of the file system module, especially the write function: https://nodejs.org/api/fs.html#fs_fs_write_fd_buffer_offset_length_position_callback
Other possible solutions to your problem:
Use a database as eol suggested
Use an append-only file where you only append the updates. This is more efficient because not the whole file has to be written.
Split your database-file into several files (this is what databases often do below the surface)
Since you are working on a file based data store, you could use FS in node.js to read and write files. The readfilesync callback returns you the whole content of the file which should be parsed as JSON and changes are to be made.
So AFAIK the only solution is to read the whole file and then update or delete the value. In the worst case scenario you will traverse O(n) when you are trying to update or deleting the key.
You've discovered why databases are so complicated ;)
There isn't really a good way to update a json file without completely reading & parsing the data, editing and then writing the result again.
If you're looking for a simple file database, have a look at sqlite
For making these types of data changes, USE DB, for JSON files you have to store full data in the variable and perform the operations.

How can I generate and serve dynamic .js files with a Node.js-server which deliver useable functions to the client?

The content of the .js file is different depending on the settings on the server and the variables provided by the client(s).
I have managed to send a dynamically generated string as a file using Express' res.type(".js") and res.send(file_as_string). I create this string by appending a stringified object behind "var name_of_an_object = " using JSON.stringify().
This works in most cases, but not for functions. Functions, whether within an object or not, magically morph into {} (empty objects). I just need a way to make the functions into strings that are immediately readable by the client when it has loaded the .js file.
Since I get a feeling there has to be a more robust way of doing this, I would love to know about it. If there isn't, I would like to know as well. Andy ideas?
I thank you in advance.

JSON schema parser Javascript

My end goal is to read any JSON schema and represent it in a tree(HTML).
For this I need a method to parse JSON schema (right?). I went through the implementations in this page and this editor which outputs an html form from JSON schema.
What I am asking is whether there is any optimum open source solution I can use or is my approach wrong?
Is there a way to get a list of properties along with their attributes?
You can use Ajv with custom keywords to create a JSON data processor/parser (JSON Schema will be used as data in your case).
You will need to define a schema with custom keywords that would be used to process your schema and generate/collect any side effects you need in the validation context (you'll need to pass this context to validation function with call/apply method and use passContext option so it is passed to subschemas and custom keywords).
This approach is used in JSONScript evaluation schema to evaluate script (but instead of script you would pass your schema as data).

Best-practice for javascript configuration on new web project

I would like to ask a question about a new and large scale web project's javascript requirements. We will use lot of javascript, ajax requests, jquery, json objects & jquery plugins in our project. We planning to store global variables and lot of default values in a global site configuration file with php class and ini file on server-side.
But we need to read, use and sometimes override some variables and configuration values on client-side with javascript - jquery.
This javascript based configuration file must have following properties;
Won't have all server-side config values. Only we need.
Must be a single file that will be call on html head section.
Must define a global variable or json or javasctipt object or array (I don't know which is best)
This values must reachable by other javascript functions and objects.
Will store booleans, strings, integers maybe a some little initialization methods for 5-6 different pages (ex.: we don't need main page's config values on product detail page's and we don't need product detail page's some initialization methods and values on main page etc.)
We need to reach some values of this configuration object on every page like debugMode=true or false etc..
We need to know in other javascript objects to running platform via this config file for images and other resource paths (Developer-Test-Stage-Production)
Also we can completely generate this file on server side or generate a static .js file and after a PHP request, set some user-page-specific or language specific values, than we must be put (override) some of this server-side generated values in Js object.
What is best-practices for this solution? Any suggestions?
Must define a global variable or json
or javasctipt object or array (I don't
know which is best)
JSON is basically an object literal, so it can do both. Go for it. Think of JSON as a serialized javascript object.
This values must
reachable by other javascript
functions and objects.
As soon as you run the JSON it will be available in your code.
Will store
booleans, strings, integers maybe a
some little initialization methods for
5-6 different pages (ex.: we don't
need main page's config values on
product detail page's and we don't
need product detail page's some
initialization methods and values on
main page etc.)
Again, JSON can do all of that.
So I would suggest a JSON file, that is included via script tag on the client side. JSON is easy to generate, read and manipulate on the server side (eg.: json_encode, json_decode in php).
It SHOULD BE a static js file, as it stresses the server the least. Also, Gzip compression can help to keep the bandwidth cost low.

Categories

Resources