Giter VIP home page Giter VIP logo

angular-api-adapter's Introduction

Angular Api

An alternative to ngResource for interacting with APIs.

Basic Usage

Set up a service.

angular
	.module("myApp", [])
	.factory('Customer', ['$rootScope', 'ApiAdapter',
	function($rootScope, ApiAdapter) {
		ApiAdapter.addRoute(
			"get_customers",
			"/api/customers"
		);
		ApiAdapter.addRoute(
			"get_customer",
			"/api/customers/{customerId}"
		);
		ApiAdapter.addRoute(
			"create_customer",
			"/api/customers",
			{
				"type": "POST",
				"success": function() {
					ApiAdapter.clearCache("get_customers");
				}
			}
		);
		ApiAdapter.addRoute(
			"update_customer",
			"/api/customers/{customerId}",
			{
				"type": "PUT",
				"success": function() {
					ApiAdapter.clearCache("get_customer");
					ApiAdapter.clearCache("get_customers");
				}
			}
		);
		ApiAdapter.addRoute(
			"delete_account_customer",
			"/api/customers/{customerId}",
			{
				"type": "DELETE",
				"success": function() {
					ApiAdapter.clearCache("get_customer");
					ApiAdapter.clearCache("get_customers");
				}
			}
		);

		return {
			get: function(params, args, noCache) {
				return ApiAdapter.execute(
					"get_customer",
					params,
					args,
					((typeof(noCache) != "boolean")? true : noCache)
				);
			},
			index: function(params, args, noCache) {
				return ApiAdapter.execute(
					"get_customers",
					params,
					args,
					((typeof(noCache) != "boolean")? true : noCache)
				);
			},
			create: function(params, args, noCache) {
				return ApiAdapter.execute(
					"create_customer",
					params,
					args,
					((typeof(noCache) != "boolean")? true : noCache)
				);
			},
			update: function(params, args, noCache) {
				return ApiAdapter.execute(
					"update_customer",
					params,
					args,
					((typeof(noCache) != "boolean")? true : noCache)
				);
			},
			destroy: function(params, args, noCache) {
				return ApiAdapter.execute(
					"delete_customer",
					params,
					args,
					((typeof(noCache) != "boolean")? true : noCache)
				);
			}
		};
	}
]);

Use the service

angular
	.module("myApp", [])
	.controller('MyController', ['$scope', '$rootScope', 'Customer',
	function($scope, $rootScope, Customer) {
		// Get a customer
		Customer.get({ customerId: 1 }).then(
			function(result) {
				// ...
			}
		);
		// Get customers
		Customer.get({}, { page: 5 }).then(
			function(result) {
				// ...
			}
		);
	}
]);

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.