Skip to content

Instantly share code, notes, and snippets.

@robwormald
Forked from anonymous/gist:8368253
Last active January 2, 2016 22:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robwormald/8368255 to your computer and use it in GitHub Desktop.
Save robwormald/8368255 to your computer and use it in GitHub Desktop.
innitApp.provider('API',function(){
//temp storage for new collection
var _collections = []
//holds model references
var _models = {}
return {
//this fires when the provider is injected
$get : ['$resource',function($resource){
angular.forEach(_collections,function(_collection){
var newCollection = $resource(_collection.definition.endpoint,_collection.defaults,_collection.actions)
_models[_collection.name] = newCollection
})
return _models;
}],
//register a collection with the provider
$collection : function(name,def){
var newCollection = {name : name, definition : def}
_collections.push(newCollection)
}
}
})
innitApp.config(['APIProvider',function(APIProvider){
//register a collection w/ api provider
APIProvider.$collection('Cops',{
endpoint : '/api/cops'
})
APIProvider.$collection('Stations',{
endpoint : '/api/stations'
})
}])
myApp.controller('testCtrl',function(API){
$scope.fatCops = API.Cops.query({isFat : true})
$scope.allStations = API.Stations.query()
$scope.createNewCop = function(){
var newCop = {name : 'Joe', isFat: false}
API.Cops.create(newCop).then(function(newlyCreatedCop){
})
$scope.moveCopToOtherStation = function(){
API.Cops.get({id : 1}).then(function(cop){
cop.station_id = 2;
cop.save().then(function(){
console.log('moved cop to new station')
})
})
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment