Coder Social home page Coder Social logo

bekerov / angularjs-mongolab-promise Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pkozlowski-opensource/angularjs-mongolab

0.0 2.0 0.0 212 KB

Resource-like factory for MongoLab based on $http and working with promises

Home Page: http://embed.plnkr.co/Y8sg4V

License: MIT License

angularjs-mongolab-promise's Introduction

Build Status

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 significantly 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).

Examples

To see it in action check this plunker: (http://embed.plnkr.co/Y8sg4V).

Usage instructions

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.all(function(projects){
     $scope.projects = projects;
  });
});

Documentation

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([options], successcb, errorcb)
    • Resource.query(criteriaObject,[options], successcb, errorcb)
    • Resource.getById(idString, successcb, errorcb)
    • Resource.getByIds(idsArray, successcb, errorcb)
    • Resource.count(criteriaObject, successcb, errorcb)
    • Resource.distinct(fieldName, criteriaObject, successcb, errorcb)
  • on an instance level:
    • resource.$id()
    • resource.$save(successcb, errorcb)
    • resource.$update(successcb, errorcb)
    • resource.$saveOrUpdate(successcb, updateSuccesscb, errorcb, updateErrorcb)
    • resource.$remove(successcb, errorcb)

Resource all and query supported options:

  • sort: ex Resource.all({ sort: {priority: 1} }, cb);
  • limit: ex Resource.all({ limit: 10 }, cb);
  • fields: { 1 - Includes field, 0 - excludes field } ex Resource.all({ fields: {name: 1, notes: 0} }, cb);
  • skip: ex Resource.all({ skip: 10 }, cb);

Contributting

New contributions are always welcomed. Just open a pull request making sure that it contains tests, doc updates. Checked if the Travis-CI build is alright.

Contributtors

angularjs-mongolab-promise's People

Contributors

novito avatar pkozlowski-opensource avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.