Ember.js & REST API - rest

From all the various examples of Ember.js, I have not been able to figure out if there is a default method in Ember.js to do REST AJAX calls. Many examples build their own interfaces for CRUD operations. I even tried to sift through the code to find any reference to AJAX calls but came up with nothing.
So, my question is, is there a default implementation of REST API in Ember.js. If yes, how do I use it? Also if, for a specific application, I want to build custom CRUD methods, where do I plug these into Ember.js?

It seems that Ember Data is what you are looking for. It is part of emberjs organiztion in GitHub.

[2014-02-18: Deprecated - I no longer support ember-rest because it is overly simplistic, and would recommend using ember-data for most ember projects. Check out the Ember guides for an overview of ember-data as well as this example project ]
While learning Ember, I decided to create a very simple Ember REST library. I also wrote an example Rails CRUD app.
My goals were to keep this project as simple as possible, while still including error handling and validation. Ember REST is certainly much leaner than Ember Data and Ember Resource, and I hope you'll find the code well commented and accessible.

There is a Ember Resource library aiming REST JSON interfaces. It provides Ember.Resource class with save(), fetch() and destroy() operations that could be easily overriden. Looks like it should be more mature than Ember Data for now.

Ember.js can work nicely with Ember Data. That said, there is a specific format of REST to follow. When followed, you can streamline the process of connecting API with Ember and have so much less work.
In case you use custom REST, the place to adjust is:
adapter - to inform from where you like to get data
serializer - how data should be adjusted for custom REST API

Related

How to consume REST with GET and POST

Is there a standard out-of-the-box way to access an API using GET or POST in TypeScript?
I only find libaries that do that like fetch or superagent suggested here when using React or HttpClient suggested here when using React.
I wonder though if there is a plain an simple way from within TypeScript to consume a REST API.
I wonder though if there is a plain an simple way from within TypeScript to consume a REST API.
Lets simplify REST API to be *I want to make GET and POST requests. The fact that it is a REST style API is not relevant.
I want to make GET and POST requests
Native ways of doing this depends on the JavaScript environment. e.g. Browser's traditional API has been XMLHttpRqeuest : https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest which is fully supported by TypeScript. A newer API is fetch which is also supported by TypeScript but might not be supported by the browser you are targetting.
On node you would use http module https://nodejs.org/api/http.html
Suggestion
Now if you want an API that works across both node and old browsers you will need some library that abstracts the native features. One suggestion is axios which works with TypeScript out of the box.

How do I send details from the form to a rest API in ember.js?

I'm relatively new to ember and I have been trying to send a POST request to REST API. Can someone explain how POST request is done in ember?
you can use ajax for REST api call
$.ajax({
type:"POST",
url:"[postURL]",
async:false,
data:{[data you need to send]}
success:function(data){
console.log(data);
}
});
Here is the sample code. Try this
https://ember-twiddle.com/e4ebddc075da81462224152fc770b983?openFiles=components.form-submit.js%2C
It depends what parts of Ember you want to use. One of the main backbones of Ember.js is the Ember Data library, which cooperates closely with Ember models, which you can read about here. If you decide not to use Ember Data (which I think most wouldn't recommend but is ultimately up to you) you can just populate your models manually, using API requests made with ajax and setting model properties with their responses data.
If you want to use Ember Data for your API requests, you will need to use an API adapter, which will let you use Ember Data methods to query your API for your models , so that you can write code like store.findRecord('book', bookId);, where the API calls are handled automatically by the adapter, and caching / storage is dealt with by Ember Data.
You can read about adapters here, and should probably just read the whole section on models to learn more about how they work whether and how you want to use Ember Data in your application.
I probably wouldn't worry about Ember Data if all you're trying to do is make a POST request.
I would just rely on the "normal" browser APIs for making network requests. These days, the easiest way to go is using the fetch API.
There's a handy Ember addon called ember-fetch that will wrap the Browser-provided function and make it play a little nicer with Ember. You can install it by doing
ember install ember-fetch
Once you have it installed, you can import the fetch function from that module and go to town!
A quick example of using this in an Ember component might look something like this Twiddle I whipped up.
https://ember-twiddle.com/2511bf1b2409da2b7ff33d88fb12552e?openFiles=components.some-submitting-component.js%2Ctemplates.components.some-submitting-component.hbs
Note that, while the code is correct, the POST will fail because it doesn't actually go to a real API.

What is the benefit of using Spring REST Docs comparing to Swagger

Spring REST Docs was released recently and the documentation says:
This approach frees you from the limitations imposed by tools like Swagger
So, I wanted to ask when Spring REST Docs is preferable to use comparing to Swagger and which limitations it frees.
I just saw a presentation here that touches on your question among other topics:
https://www.youtube.com/watch?v=k5ncCJBarRI&t=26m58s
Swagger doesn't support hypermedia at all / it's URI centric
Swagger's method of inspecting your code can lag behind your code. It's possible make a change in your code that Swagger fails to understand and won't process properly until Swagger gets updated.
Swagger requires lot of annotation, and it's painful to include the descriptive text you want in an api document in annotations.
There are just some things that Swagger can't figure out from inspecting your code.
In any case, these are just a couple of points. The presenter does a much better job discussing it than I could.
I thought I would chime in to give a little bit more context surrounding Swagger, what it is, and what it is not. I believe this might help answer your question.
Swagger 2.0 is being adopted by a lot of big names and big platforms like Microsoft Azure, Paypal, SwaggerHub.com, DynamicApis.com, etc... Something to keep in mind is that Swagger is very simply a specification. It is not a framework. There are a lot of frameworks out there built to generate Swagger output that crawl through your code looking at your API information in order to build the Swagger 2.0 JSON file that represents your API. The Swagger UI that you see your APIs on is driven directly from this Swagger 2.0 JSON file. fiddler it to check it out
It is important to note that a framework that was created to allow you to "use swagger" is not how Swagger has to work (i.e. it is completely up to the implementation of the 3rd party framework). If the framework you are using to generate your Swagger 2.0 documents and UI is not working for you then you should be able to go find another framework that generates the Swagger artifacts and swap the technologies out.
Hope this helps.
From Spring REST docs:
The aim of Spring REST Docs is to help you to produce documentation for your RESTful services that is accurate and readable
This test-driven approach helps to guarantee the accuracy of your service’s documentation. If a snippet is incorrect the test that produces it will fail.
Spring REST docs advantages:
Documentation is written in the test code so it does not overload main code with lots of annotations and descriptions
Generated docs and examples are accurate because related test must pass
Docs can provide more specific and descriptive snippets
Format is suitable for publishing
Spring REST docs disadvantages:
Requires more work
Documentation provides request/response examples but don't provide interactive tools to modify and try out requests
Swagger advantages:
Quick, automated generation from a code
Interactive request execution - can be used for acceptance testing
Built around the OpenAPI Specification
Swagger disadvantages:
For more descriptive documentation it will require a lot of annotations
Tests are not related to the documentation so sometimes documention may deviate from reality
There is some limitation with swagger and the specific spring stack.
For example : with "param" in your Request Mapping you can define more than one method with the same url ans so simplify your code.
But swagger show you just one method
One disadvantage with Swagger is: it cannot handle models which have cyclical dependencies. If a model has cyclical dependency and if swagger is enabled, then spring boot server crashes.

AngularJS OR BackboneJS from Webservice point of view

We are trying to rebuild an app using Backbone or Angular JS. Main criteria for selecting either of them would be:
The Java script Framework should be able to make calls via SOAP as well as REST.
My question is based on the criteria's which framework should I be choosing? I know angular as well as Backbone are good for REST web service. I am not really sure how well they do when we are trying to make SOAP calls. Not much help online either. Has anyone successfully implemented SOAP calls via Angular or Backbons JS?
Backbone.sync method can be easily overriden to make SOAP calls, but it will fall apart if your SOAP service manages anything else than resources, as REST calls in Backbone are made on behalf of either models or collections of models that support the classic CRUD semantics. As for AngularJS, I haven't used it enought to provide you with an informed answer.

AngularJS official tutorial, how to add object?

I'm kinda stuck at testing Restful model without server-side.
In Google official tutorial step 11
They show real nicely how to get all the phone lists from a local json files using get method.
It is really nice and very simple, but they are lacking something very basic in this tutorial deleting/adding with delete/post methods.
I'm stuck right now because I cannot find a way to add/delete objects using Restful model without implementing server-side DB, of course not permanently just throughout the application life.
Can someone give an example that's built on AngularJS official tutorial, how to implement a simple add of a phone object?
You want to use Angular's $resource service.
Documentation here
That contains a working example similar to what I think you're asking.