Mastering Web Application

(Rick Simeone) #1

Communicating with a Back-end Server


Getting familiar with the data model and MongoLab URLs


The data model of the sample SCRUM application is rather simple and can be
illustrated on the following diagram:


Project

Sprint

BacklogItem Task

User

team member*
* *
* *

There are five different MongoDB collections holding data of users, projects and
project-related artifacts. All the data are accessible through the RESTful interface
exposed by MongoLab. The REST API can be invoked by targeting URLs with a well-
defined pattern:


https://api.mongolab.com/api/1/databases/[DB name]/collections/
[collection name]/[item id]?apiKey=[secret key]


All the REST API calls targeting databases hosted on MongoLab need
to include a request parameter named apiKey. The apiKey parameter,
with the value unique to an individual account, is necessary to authorize
MongoLab REST API calls. The complete description of the REST
API exposed by MongoLab can be consulted at https://support.
mongolab.com/entries/20433053-rest-api-for-mongodb.

$http APIs quick tour


Doing XHR and JSONP calls using the $http service is straightforward. Let's
consider an example of fetching JSON content with a GET request:


var futureResponse = $http.get('data.json');
futureResponse.success(function (data, status, headers, config) {
$scope.data = data;
});
futureResponse.error(function (data, status, headers, config) {
throw new Error('Something went wrong...');
});
Free download pdf