Skip to content

Instantly share code, notes, and snippets.

@janmonschke
Last active December 17, 2015 14:49
Show Gist options
  • Save janmonschke/5626866 to your computer and use it in GitHub Desktop.
Save janmonschke/5626866 to your computer and use it in GitHub Desktop.
myApp = angular.module 'myApp', []
# encapsulates ajax stuff
myApp.factory 'myService', ($http, $rootScope) ->
data = { test: 123 }
url = 'http://192.168.2.4:8080/'
# doesn't do anything, no request in the server logs
postItNormal = ->
$http.post url, data
# works perfectly, but isn't the angular-way
postItJqueryStyle = ->
$.ajax
url: url
data: data
type: 'POST'
dataType: 'JSON'
# found $scope.$apply somewhere on google, but I had to stick to $rootScope here
# why does it work this time? a request is logged...
postItThroughRootScope = ->
$rootScope.$apply ->
$http.post url, data
return {
postIt: postItThroughRootScope
}
# the controller
GestureController = ($scope, myService) ->
$scope.action = ->
myService.postIt()
var GestureController, myApp;
myApp = angular.module('myApp', []);
myApp.factory('myService', function($http, $rootScope) {
var data, postItJqueryStyle, postItNormal, postItThroughRootScope, url;
data = {
test: 123
};
url = 'http://192.168.2.4:8080/';
postItNormal = function() {
return $http.post(url, data);
};
postItJqueryStyle = function() {
return $.ajax({
url: url,
data: data,
type: 'POST',
dataType: 'JSON'
});
};
postItThroughRootScope = function() {
return $rootScope.$apply(function() {
return $http.post(url, data);
});
};
return {
postIt: postItThroughRootScope
};
});
GestureController = function($scope, myService) {
return $scope.action = function() {
return myService.postIt();
};
};
RunLink
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment