How to get selective data from multiple json files? - javascript

Let suppose I have 5 json files in which data is stored.
Now I have 5 different logins(different users), and for each different login, seperate json data should be loaded. So, how can I implement this?
can I implement this without getJson() ??
I want to store it as 5 different js files with data as variables in it.
Any ideas?
I mean, I want to get variable from particular json file (same variable used in all json files)

of course you can.
As you mention, users should be logged in. So to each user can be attached specified attribute containing associated with him js file with data.
Or even simplier. JS file can be named as user login. So after login it can be loaded withot any troubles.

Related

How to save click path as JSON?

Let's say I have a quiz written in JavaScript. There are four different possible answers to each question. The participant clicks his way through the quiz one by one, he can cancel it at any time.
Now I want to save the user's click path including the respective timestamps. My idea is to record the click path as a JavaScript object and to transfer this object to the server via AJAX and save it in a JSON file after each click. Does that make sense?
The tricky things seem to me to be (1) to update the correct object (the correct line(s) in the JSON file) within a quiz session (no session cookies) for each click from the second click and (2) to append a new object for a new quiz session, both, if possible, without reading and rewriting the entire JSON file every time.
Your opinions and ideas are appreciated.
Edit: I have control over the backend and I'm using PHP.
Now I want to save the user's click path including the respective timestamps. My idea is to record the click path as a JavaScript object and to transfer this object to the server via AJAX and save it in a JSON file after each click.
Rather than sending the object to the server via Ajax and saving it in a .json file, you can, more simply, store a JSON (including timestamps) in localStorage and update it there.
See the localStorage API:
https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
localStorage is both:
more robust / persistent than keeping the data in an object
less elaborate than Ajax-ing data to the server repeatedly

How to transfer access token from 1 javascript file to another on the same html page without giving the access to the user to read it?

I have a form of 6 fields in HTML and an import button which import a JSON file. The file contains 8 elements 6 of which are the part of the HTML form and the other 2 are some confidential information. Now as soon as I import the JSON file, the form gets auto filled using the JSON elements. I can change any of the field using the form. There is another button named save which saves the 6 fields from the form in a new JSON file. Now the problem is that How to pass the 2 confidential fields from one JSON file to another without giving the user access to read them. I have read about passing data using Local storage/ Session storage and Cookies. I think using local storage is not a good idea for this case but I am not sure about cookies. What should I do?

Processing a file upload with javascript before saving to database in Cakephp

I have a form that includes a file upload.
I need to extract some meta information using javascript from the file before saving the contents of the form to my database.
What would be the best way, if any, of achieving this?
To clarify: The issue isn't the with extracting the file meta but rather how to access the $_FILES array and execute a js file with this data before finally allowing the form to submit to the server.
How I am achieving this currently:
I submit the form to the controller action
It saves the data and then it sets a view variable with the location of
the uploaded file before re-rendering the initial view upon successful form submission
The view checks if the variable is set, and if it is, executes the
javascript which basically extracts the meta now that it can access where the file is, and updates the db
with an ajax call.
The above method is not ideal in that the meta belongs to the same row of data that is saved when the form first submits so ideally I would like to include it initial form submission and not when the page is rendered again after the form has been submitted. Seems like a bit of a dirty hack to me but I can't as of yet see another way this can be achieved.
I would suggest uploading using ajax (SWFUpload/Uploadify) and then sending data back to the view with the processed file data.
You can upload the file, issue a number of callbacks, and finally return the rendered filedata back to the waiting javascript function that issued the request.
Just my two cents
What sort of metadata? To answer your question, yes, this is possible, but only if the browser supports the File API. This excludes IE9 and older. For more information, see this MDN article on FileReader. There are various libraries that will extract specific types of metadata from files. For example, if you are interested in parsing EXIF metadata from an image file, you should look into something like jQuery-fileExif.

Accessing Session Variables in Single Page Applications

I'm developing a simple(!) 2 page application.
Page1: Takes basic details from the user about the program is creating for his trip - name of the trip, dates etc.
After clicking Save, a entry is created on MongoDB using controllers in Lithium PHP for the program. A id is generated and passed on to page 2 as a parameter in URL like so:
$this->redirect('/iplans/save/' . $program_id . '/' . $program_name);
Page 2: is a single page application using many Backbone scripts (hosted in individual js files) and allows the user to add datewise details of his program. No js script is in-line.
I want the backbone collection to be saved on the server side when Finish is pressed but need to send in the program_id with this collection so that the right program is updated in MongoDB.
Questions:
How does one ensure that the program_id from lithium/php server side is picked up by backbone javascripts that are in their own js files (not inline)
What other options are available besides Controller::redirect to forward the control from page1 to page2?
What other options can be used to pass-on the program_id and any other values that may be needed to backbone/JS layer. Is there a standard-way of passing such items?
There are a couple of options:
Parse out program_id from window.location.href
Dynamicly create a bit of javascript in your lithium view. it could be as simple as:
"<script> myvars = " . json_encode(array(...)) . "<script>"
and then you can read the variables from the javascript.
Create the onDomRady function dynamically from lithium
That is a good way. That is, one page show form and receive data from the form, and one page that show some data. They are not related to each other, and it will make the code easy to understand.
You could use AJAX/JSON to send/receive data to/from the server. Also see answer 1b,1c
If it's just a variable being written to the session that you need to access in a single-page app, just make a controller (i.e. SessionsController) that returns the current session information, and turn content negotiation on.
Then you can get whatever keys you need any time.

parse external json source to html

I need to get some data from a json file stored at another server than my own. I've received the url of the file but have to "anonymize" it below, the url is exactly as below but with a different domain.
http://api.somedomain.com/v1/26963723e61eae69cefd/companies/5565592010/related-articles.json?languages=nor&per_page=10&page=2
Now I want to render the data in that file into a list or table structure in my html file, preferably after the page has loaded. If jQuery can be used for this then that would be great otherwise regular javascript will do.
Unless someone can toss up a solution I'd be happy to take pointers on where to turn to learn about how this can be done.

Categories