AngularJS: Import text [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am not a javascript person so please apologize if this question doesn't make sense:
In my angular project/app.js I have a an array articles which are dictionaries of article elements (description, author, etc...)
var articles = [
{ title : "Article 1",
date : date.toDateString(),
shortDesc : "Short description...",
...
},...
]
This variable is then assigned to an instance variable of the controller
app.controller('BlogController', function () {
this.articles = articles;
});
Now, instead of typing my actual short description into the article dictionary I would like import that text from another file. How do I do that?
Thanks so much in advance!

You can have json file and make call to it by $http.get('data.json'). But then again you're unable to post data to it (client side difficult to write data to file). Else you can install json-server npm module & serve your json file on it, then you can do all CRUD operation through angular's $http service. This's just for mocking purpose or faking REST API, for real project purpose you should have real web services handled by server side technologies (e.g. Java, NodeJS) or create on cloud backend-as-a-service like Firebase, AWS (dynamoDB).

Related

Testing the API from the client side [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I've been working within a project for a couple of months now and met several issues, when the API response changed by the backend guys and it was structured in a way that I didn't expect it to be structured.
The tests that we have mocks the API responses, so they all pass and there is nothing wrong when deploying the app in that state :(
My question is, as I'm quite new to the testing - is it a common thing to do to test the API responses themselves?
If yes - are there any JavaScript libraries that can help me with that?
If no - how to make sure that the response are structured that way we want them to be?
What you need is to add integration tests to test un-mocked API responses.
AND to prevent unexpected API response structure, you should use a versioned API. So as long as you do not upgrade your API version to a new major version, the API response should not be unexpected.
As you are using Jest you can add your integration test using this framework
You can use the documentation lib like swagger where you can check the structure of the api response.
Refer: https://swagger.io/solutions/api-documentation/

Simple local search function. Javascript and JSON? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Building a 'fake' web browser and web pages that are all compiled locally in a windows form application. The whole thing operates offline.
I need to build a search function on one of the web pages I've made. Obviously theirs no server involved, so I have to have the data extracted and displayed from a local source. I've used 'Jput' before in a previous project so i was thinking about having a Json file that can be called. The site is a fake social network, so when a user searches a name or phrase, it displays related results (profiles) either on a separate page or within the current doc. Jput is good, but its pretty limited when it comes to styling.
QUESTION
What would be the best way of doing this? How would I index all these results and how would the search engine work?
Let me know if more detail is needed for this question
Thanks!
You can use a normal Javascript array ( that can be stored as json) :
Example data:
var users=[
{name:"John",age:22}
{name:"Jack",age:23}
]
Now you can search after name:
document.body.innerHTML=users.filter(el=>el.name.split("Ja")[1]).map(el=>el.name).join("<br>");
This will show all users that names contain "Ja"inside of the document ( or any other html elem, can be based on users input).
You can easily show a users profile like this:
user=users.find(el=>el.name=="Jack");
for(key in user){
alert(key+":"+user[key]);
}
This is just the beginning of what you can do with natives js, no need for libs etc.

What's the best practice for having a Laravel application serve both the browser-based application, and act as an API for the iPhone and Android app? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
We use repository pattern in Laravel 5.1 and kept our logic in repository and we return the data from repo to controller, how can we use same data for Native app and web browser (i.e. web application).....off course data is in json so how can we show same data in blade template...do we need to use another framework for front end (Angular or any other)?
I personally don't think you would need a package or framework to achieve this. Since you are following the repository pattern, your controllers are only in charge of returning results (optionally transforming them for output too).
To achieve your goal;
1- Group your API routes (etc. Route::group('api/v1', ...)
2- Create a class or even better a Trait to detect incoming routes
3- Either within your controller or on the class that handles JSON conversion, detect the route of the request.
4- If the request is being made from the api route, return JSON. Blade template otherwise.
You can also check out this link, which will help you building up your route detection. Also remember that you would need this exact implementation wihin your ExceptionHandler as well. You wouldn't want someone to see a json error message on your web front end, or a 404 blade template being returned from your API.

RESTful Commit to Github file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I've been looking for quite some time if it is even possible to do this.
I'd like to make a RESTful POST call with a javascript without the use of a server (node) to send a json object in the request body to update a json file in a github repo triggering a commit/push.
Yes, it's possible to do this using the Github API.
The URL must be something like that:
https://api.github.com/repos/{repositoryName}/contents/{path}
repositoryName is the repository where you want to put your file and path its path within this repository.
Regarding the authentication, you need to follow this link https://developer.github.com/v3/#authentication. I successfully tested with basic auth.
You need then to use an HTTP method PUT with content with the following structure:
{
"message": "a commit message",
"content": "bXkgbmV3IGZpbGUgY29udGVudHM="
}
The field content corresponds to the content of your file encoded with base 64.
Hope it helps.
Thierry

Writing an API using bottle/flask to build data visualizations [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to write an API that will read the data fed through a UI, filter the data and feed the data to a set of Javascript (D3) visualizations that I created and output the result as SVG/PNG/PDF.
How am I going to implement this ?
Let me know if more details are required for you to understand and answer the question.
Any help would be highly appreciated.
There is the HTML 5 drag and drop option that is already explained. But if you insist on getting a remote server involved then when you get data through you UI you need to save it in a file or db and then in your back end you clean the data (I assume your cleaning process is happening in Python for example) and then you have a cleaned data ready to be passed. Here you need flask and templates to pass the data to your HTML and evoke D3. If you take a look at Flask quick start then you'll see how flask comes in handy to mediate communication between a backend code and your HTML. From there you get a better idea. I also suggest reading this blog that explains a mini blog application using flask this covers API, Flask part of your question very well.

Categories

Resources