Skip to content

Instantly share code, notes, and snippets.

@saintc0d3r
Created December 8, 2014 22:26
Show Gist options
  • Save saintc0d3r/293b30ced19f59b18765 to your computer and use it in GitHub Desktop.
Save saintc0d3r/293b30ced19f59b18765 to your computer and use it in GitHub Desktop.
HOW TO - Create a parameterised AngularJS service by using provider method
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="css/style.css" rel="stylesheet">
<!-- angularjs js -->
<script src="lib/angular/angular.js"></script>
<script src="lib/angular-resource/angular-resource.js"></script>
</head>
<body ng-app="awesomeapp">
<div ng-controller="main_controller">
<ul ng-repeat="item in items">
<li>
<h2>{{item.title}}</h2>
<p>{{item.description}}</p>
<h2>{{(item.price|currency)}}</h2>
</li>
</ul>
</div>
</body>
<!-- App's JS -->
<script src="js/app.js"></script>
<script src="js/services/items-datasources.js"></script>
<script src="js/controllers/main-controller.js"></script>
</html>
angular.module('awesomeapp.services', ['ngResource'])
.provider('ItemsDatasource', function(){
// a flag to determine whether the consumer is going to pull mock data or data from a REST endpoint
this.UseFakeDataource = false;
// parameter to define the REST endpoint's URL
this.RestEndpoint = '';
this.$get = ['$resource', function($resource){
// return fake items datasource
if (this.UseFakeDatasource){
// Create and return the fake items-datasources stub
return {
/**
* Get all fake items
*/
get_all : function(callback){
var fakeItems = [
{
"title": "Intel Core i7-4790K Haswell",
"description": "Desktop i7 Quad-Cores CPU @ 4.0GHz LGA 1150",
"price": 339.99,
"thumbnail": "https://s3-ap-southeast-1.amazonaws.com/pc-shop-demo/thumb_i7_4790K.jpg",
"id": "54544c9bc6c73d2c04542c6a"
},
{
"title": "SAMSUNG 840 EVO MZ-7TE500BW",
"description": "2.5' 500GB SATA III TLC Internal Solid State Drive",
"price": 239.99,
"thumbnail": "https://s3-ap-southeast-1.amazonaws.com/pc-shop-demo/thumb_samsung_evo840_500gb_ssd.jpg",
"id": "54544cbac6c73d2c04542c6b"
},
{
"title": "ASUS RAMPAGE V EXTREME LGA 2011-v3",
"description": "Intel X99 SATA 6Gb/s USB 3.0 Extended ATX",
"price": 473.99,
"thumbnail": "https://s3-ap-southeast-1.amazonaws.com/pc-shop-demo/thumb_asus_rampageV_extreme_lga2011v3_mobo.jpg",
"id": "54544cd0c6c73d2c04542c6c"
},
{
"title": "G.SKILL Ripjaws X Series 8GB ",
"description": "(2 x 4GB) 240-Pin DDR3 SDRAM DDR3 1600 (PC3 12800) Desktop RAM Modules",
"price": 70,
"thumbnail": "https://s3-ap-southeast-1.amazonaws.com/pc-shop-demo/thumb_gskill_ripjaws_8gb_DDR3_1600.jpg",
"id": "54544ce2c6c73d2c04542c6d"
},
{
"title": "CORSAIR CXM series CX600M 600W ATX12V v2.3 SLI CrossFire",
"description": "80 PLUS BRONZE PSU",
"price": 69.99,
"thumbnail": "https://s3-ap-southeast-1.amazonaws.com/pc-shop-demo/thumb_corsair_cx600M_psu.jpg",
"id": "54544cf1c6c73d2c04542c6e"
}
];
callback(fakeItems);
}
}
}
// return actual items datasource stub
var that = this;
return {
/**
* Get all items from REST endpoint
*/
get_all: function(callback){
var items_resource = '/items';
$resource(that.RestEndpoint+items_resource, {}).query(function(response){
callback(response);
});
}
}
}];
})
angular.module('awesomeapp.controllers', ['awesomeapp.services'])
.config(function (ItemsDatasourceProvider) {
// Set the flag parameter to true would return In Memory Items Datasource
ItemsDatasourceProvider.UseFakeDatasource = false;
ItemsDatasourceProvider.RestEndpoint = "http://localhost:3000/api";
})
.controller('main_controller', function($scope, ItemsDatasource){
$scope.items = function(){
ItemsDatasource.get_all(function(items){
$scope.items = items;
});
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment