-
-
Save robwormald/8368255 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
}) | |
}]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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