Last active
August 29, 2015 14:05
-
-
Save scottnath/93dd9d3dae02aac7dbeb to your computer and use it in GitHub Desktop.
Angular service to talk to Drupal
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
## Service Script | |
define(['angular'], function (angular) { | |
'use strict'; | |
angular.module('atlasApp.services.Drupal', ['restangular']) | |
.config(function(RestangularProvider){ | |
RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) { | |
var extractedData; | |
if (operation === 'getList') { | |
extractedData = data.list; | |
} else { | |
extractedData = data; | |
} | |
return extractedData; | |
}); | |
RestangularProvider.setBaseUrl('http://drupalapi.example.com'); | |
RestangularProvider.setRequestSuffix('.json'); | |
RestangularProvider.setRequestInterceptor( | |
function(elem, operation){ | |
if(operation === 'put'){ | |
elem._id = undefined; | |
return elem; | |
} | |
return elem; | |
} | |
); | |
}); | |
}); | |
## Controller Script | |
define(['angular'], function (angular) { | |
'use strict'; | |
angular.module('myApp.controllers.ContentTypeCtrl', ['restangular']) | |
.controller('ContentTypeCtrl', function ($scope,Restangular,$route) { | |
var contentType = $scope.contentType = $route.current.params.a; | |
// creates allContentItems which contains...wait for it...ALL THE CONTENT TYPE'S ITEMS! | |
$scope.allContentItems = Restangular.all(contentType).getList().$object; | |
}); | |
}); | |
## Routing in app.js | |
... | |
.config(function ($routeProvider) { | |
$routeProvider | |
.when('/', { | |
templateUrl: 'views/main.html', | |
controller: 'MainCtrl' | |
}) | |
.when('/:a', { | |
templateUrl: 'views/content-type.html', | |
controller: 'ContentTypeCtrl' | |
}) | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment