Backbone.js async calls - backbone.js

I've been searching about this and haven't found an answer yet.
I found out that you can use async: true when calling fetch on Backbone, which isn't mentioned in its website. Can other methods, such as save, create, etc., be used with async too?

Have a look at Backbone.sync documentation :
Backbone.sync is the function that Backbone calls every time it
attempts to read or save a model to the server. By default, it uses
jQuery.ajax to make a RESTful JSON request and returns a jqXHR.
It means you can use all jQuery.ajax options on you requests, and you can indeed set an async option (defaults to true) on save/create

Related

Get raw request / response from $http call

I am writing an AngularJs app to test an API we developed. The app uses the $http object to make requests to the API. One of the asks is that after the call it's possible to review the raw HTTP (headers and bodies) Request/Response, similarly to what's available in Fiddler via Raw tabs.
Is it something that $http provides out of the box?
If not, it appears that the only challenge is gaining access to the actual request http headers. It's easy to get the response headers and request/response bodies, but not sure how to get the actual request headers.
Thanks.
If you are using $http service to make your API calls, you can use Interceptors to achieve what you want.
Here is what docs tell us about them:
For purposes of global error handling, authentication, or any kind of synchronous or asynchronous pre-processing of request or postprocessing of responses, it is desirable to be able to intercept requests before they are handed to the server and responses before they are handed over to the application code that initiated these requests. The interceptors leverage the promise APIs to fulfill this need for both synchronous and asynchronous pre-processing.
You can find more in depth explanation in the official docs. For example, here.
Also, there are some questions about interceptors on this site. There are some examples of their usage for displaying loading screen in applications: here, here and, probably, somewhere else.
Hope, this helps.
Yes, AngularJs is wrapped around some JQuery or internally JQlite if JQuery is not present and written in Javascript and it provides some pre-defined services. A typical service looks like the following.
AngularJS docs: tutorial step 5
$ Prefix Naming Convention You can create your own services, and in
fact we will do exactly that in step 11. As a naming convention,
Angular's built-in services, Scope methods and a few other Angular
APIs have a $ prefix in front of the name.
The $ prefix is there to namespace Angular-provided services. To
prevent collisions it's best to avoid naming your services and models
anything that begins with a $.
If you inspect a Scope, you may also notice some properties that begin
with $$. These properties are considered private, and should not be
accessed or modified.
angular.module('myApp')
.factory('myService', function ($http, $injector) {
'use strict';
return $http.get('/endpoint')
.then(function () {
return $injector.get('endpoint');
}
.error(function () {
// handle error
}
};
})
Have a look a the image in AngularJS docs which shows a number of services with the $ prefix. Mostly, wrappers over service. It is reserved. More at FAQ.

Angular $resource.save() not working?

I am trying to create a web app with Angular, and I need some help. I need to POST data to an api. Rather than using $http (which works), I'd like to use $resource, and save().
I've also used GET and query so far, and they work fine. But when I try to call save(), I never get a response. I also don't see a POST request go through when I check on the server side.
Here's my code for the $resource factory (normally with my actual API url, of course)
.factory('ExamplePosts', function($resource) {
return {
all: $resource('http://my.website.com/api/')
};
});
In my controller, I can run ExamplePosts.all.query() or .get(), and retrieve the results. (I also see the request in my server logs.)
I then tried running ExamplePosts.all.save(), and I did not see a POST request on the server logs. I can post to it through forms and it works, but there seems to be a problem with Angular.
Appreciate any help I can get, thanks!
You actually need to add a prefix "$" to the save method as .$save() not .save() read this for more on those that need the $prefix read more

With Angular js's promise defer implementation do we need WEB API's to be async?

Angular JS supports Promise Defer functionality which is asynchronous. If I have async support on the client side, do I still need to expose async methods via WEB-API?
Making the client async improves the user experience, because user doesn't have to wait for every request.
But using async methods in the server can make the server faster and able to manage more requests. It is the same as in the client, if you don't block the main thread you are able to do other things while the other operation is executing.
You can implement async methods in one or in both sides. In each side you can get different benefits.
In this other question there are more information that explais when it is important to use async methods: Why should I create async WebAPI operations instead of sync ones?

Angular $http transformResponse and cache

Is the response from an Angular $http request cached before or after any transformResponse functions have been applied?
Use case:
My client's REST API returns a lot of metadata (most of which I don't need) so I don't want to cache the entire response. I'd like to apply a specific transformResponse for each different API end-point that copies only the fields I need into a new object\array and dispose of the original. I would then like the new object to automatically be cached. This should be nice and easy if the return data from transformResponse is cached.
Apparently is cached before transforming, as you can see in $http#717 sendReq is called before transformResponse, and in $http#958 you can see sendReq does the caching
Maybe you can be sure by doing a test

Setting the JSONP callback function in AngularJS

I'm trying to fetch data from a web API via Angular $resource service. The service exposes JSONP interface, but does not allow setting the callback name. Everything works well, my requests goes out, the data returns, the script is injected and then it fails because the callback function is not defined.
Angular documentation is very sparse on this, but it seems that the default callback function Angular sets up is: JSON_CALLBACK, and there's no info how to change that so that it matches the function returned by the foreign API.
Thanks.
I don't think that there is any provision to override that callback.
$resource is high level Restful api based on $http service.
You can use $http apis which returns http promise object and letting you write your success callback wherein you can process data returned from ajax request.
e.g. http://docs.angularjs.org/api/ng.$http#jsonp

Resources