Skip to content

Instantly share code, notes, and snippets.

@owenmead
Last active December 27, 2015 17:19
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 owenmead/7360879 to your computer and use it in GitHub Desktop.
Save owenmead/7360879 to your computer and use it in GitHub Desktop.
angular.module('myApp')
.controller('PrimaryCtrl', function($scope, $routeParams, investigationModel) {
var investigationId = $routeParams.id;
$scope.investigation = investigationDetailModel.investigation;
$scope.another_function = function() {
// blah blah blah
$scope.investigation.$save();
}
});
angular.module('myApp')
.controller('SmallCtrl', function($scope, investigationModel) {
var investigationId = $routeParams.id;
investigationDetailModel.set_investigation(investigationId);
$scope.investigation = investigationDetailModel.investigation;
$scope.perform_save = function() {
$scope.investigation.$save();
}
$scope.notes = investigationDetailModel.investigation.notes;
$scope.addNote = function() {
var newNote = investigationDetailModel.investigation.notes.$add({note:$scope.newNote});
newNote.$save(function() {
$scope.newNote = '';
});
};
});
angular.module('myApp')
.service('investigationModel', function investigationCharts(investigations, notes ...) {
model.set_investigation = function(investigationId) {
var ModelResource = function() {};
ModelResource.prototype = investigations.prototype;
// Extra data on the investigation here...
// Code to pull it in omitted to keep things simple
ModelResource.prototype.notes = [];
ModelResource.prototype.notes.$add = function() {
// Mimics the notes resource, and also adds it to the collection
}
// Create our new model, and
model.investigation = new ModelResource();
model.investigation.$promise = investigations.get({id:investigationId}).$promise.then(function(investigation) {
angular.extend(model.investigation, investigation);
return model.investigation;
});
};
return model;
});
angular.module('myApp')
.factory('investigations', function($resource, $http, config) {
// Actual data transform left out to keep things simple
var data_transform = $http.defaults.transformResponse.concat(function(data) { return data });
var actions = {
'get': {method:'GET', transformResponse: data_transform},
'query': {method:'GET', isArray:true, transformResponse: data_transform},
'save': {method:'POST', transformResponse: data_transform}
};
return $resource(
config.API_BASE_URL + '/investigations/:id',
{id: '@id'},
actions
);
});
// Notes service very similar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment