Skip to content

Instantly share code, notes, and snippets.

@oscarr-reyes
Created September 19, 2016 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oscarr-reyes/bf2eefa32c7475a73dc64842616c8c91 to your computer and use it in GitHub Desktop.
Save oscarr-reyes/bf2eefa32c7475a73dc64842616c8c91 to your computer and use it in GitHub Desktop.
Factory example using ngrestful
(function() {
'use strict';
// Pass the accountsFactory to the app
angular
.module('y')
.factory('accountsFactory', accountsFactory);
// Define the accountsFactory
function accountsFactory($resource) {
// Inject with ng-annotate
"ngInject";
// Define resource instance
var resource = new $resource("accounts");
// Define the account factory object to return
var accountsFactory = {
index: index,
show: show,
store: store,
update: update,
destroy: destroy,
};
// Return the account factory
return accountsFactory;
/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| Declaring all functions used in the accountsFactory
|
*/
// Display a listing of accounts.
function index() {
return resource.fetch(accountBase)
.then(function(data){ return data.data; });
}
// Display a specified account.
function show(id) {
return resource.fetch(id)
.then(function(data){ return data.data; });
}
// Store a newly created account in storage.
function store(data) {
return resource.save(data)
.then(function(data){ return data.data; });
}
// Update the specified account in storage.
function update(id, data) {
return resource.update(id, data)
.then(function(data){ return data.data; });
}
// Remove the specified account from storage.
function destroy(id) {
return resource.delete(id)
.then(function(data){ return data.data; });
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment