Promise-aware MongoLab $resource for AngularJS
##Introduction
This repository hosts a Mongolab $resource-like adapter for AngularJS. It is based on $http and is working with promises.
This is a small wrapper around the AngularJS $http that makes setting up and working with MongoLab easy. It has an interface very similar to $resource but works with promises. It significient reduces the amount of boilerplate code one needs to write when interacting with MongoDB / MongoLab (especially around URLs handling, resource objects creation and identifiers handling).
To see it in action check this plunker: (http://embed.plnkr.co/Y8sg4V).
Firstly you need to include both AngularJS and the mongolabResourceHttp.js
script from this repository (see examples above for the exact URLs).
Then, you need to configure 2 parameters:
- MongoLab key (
API_KEY
) - database name (
DB_NAME
)
Configuration parameters needs to be specified in a constant MONGOLAB_CONFIG
on an application's module:
var app = angular.module('app', ['mongolabResourceHttp']);
app.constant('MONGOLAB_CONFIG',{API_KEY:'[your key goes here]', DB_NAME:'angularjs'});
Then, creating new resources is very, very easy and boils down to calling $mongolabResource
with a MongoDB collection name:
app.factory('Project', function ($mongolabResourceHttp) {
return $mongolabResourceHttp('projects');
});
As soon as the above is done you are ready to inject and use a freshly created resource in your services and controllers:
app.controller('AppController', function ($scope, Project) {
Project.query(function(projects){
$scope.projects = projects;
});
});
Since this $resource-like implementation is based on $http
it accepts callbacks in its methods calls and returns a promise.
Each resource created with the $mongolabResourceHttp
will be equipped with the following methods:
- on the class level:
Resource.all(successcb, errorcb)
Resource.query(criteriaObject, successcb, errorcb)
Resource.getById(idString, successcb, errorcb)
Resource.getByIds(idsArray, successcb, errorcb)
- on an instance level:
resource.$id()
resource.$save(successcb, errorcb)
resource.$update(successcb, errorcb)
resource.$saveOrUpdate(successcb, errorcb, updateSuccesscb, updateErrorcb)
resource.$remove(successcb, errorcb)