Reputation: 3060
I am hacking an example angularjs app created using coffeescript. I'm new to both but have a fair amount of xp with python.
How can I call flask from within a coffeescript script?
Specifically, the eg app instantiates an array:
.controller('tableCtrl', [
'$scope', '$filter'
($scope, $filter) ->
# filter
$scope.stores = [
{name: 'Nijiya Market', price: '$$', sales: 292, rating: 4.0}
{name: 'Eat On Monday Truck', price: '$', sales: 119, rating: 4.3}
...
]
...
and then a bunch of other setup on the stores
array. Instead of this static list in the coffeescript file, I want to call a python script from within this script (which fetches rows from a DB).
Any help would be appreciated - such as a good online resource I can read. I can't seem to find any examples.
Upvotes: 0
Views: 782
Reputation: 5707
you want to use the builtin $http service to make the call to the server. For some sugar you have the option of using the $resource service which wraps the $http service. Ideally you'd create a method in a service of your own making to call from your controller..
my controller (with my service injected)
angular.module 'app'
.controller 'RegisterFormCtrl', (UsersService) ->
UsersService.create(@details)
.then()....
my service (with my resource injected)
angular.module('app')
.factory 'UsersService', (UsersResource) ->
new class
constructor: ->
@response = null
# register
create: (user) ->
UsersResource.save(user).$promise
my resource
angular.module('app')
.factory "UsersResource", (Resty) ->
Resty "/api/users/:id", {id: '@id'}
.factory "UserKeysResource", ($resource) ->
$resource "/api/userkeys"
my resource actually uses another service of mine which uses the angular $resource service..
angular.module('app')
.factory "Resty", ($resource) ->
(url, params, methods, options) ->
methods = angular.extend {
update:
method: 'PUT'
create:
method: 'POST'
}, methods
options = angular.extend { idAttribute: 'id' }, options
resource = $resource url, params, methods
resource.prototype.$save = () ->
if this[options.idAttribute]
this.$update.apply this, arguments
else
this.$create.apply this, arguments
resource
ok, so the bare minimum alternative implementation, in your controller, would be the following..
.controller('tableCtrl', [
'$scope', '$filter', '$http',
($scope, $filter, $http) ->
# filter
$http.get('/someUrl').
success(
(data, status, headers, config) ->
# this callback will be called asynchronously
# when the response is available
$scope.stores = data
).
error(
(data, status, headers, config) ->
# called asynchronously if an error occurs
# or server returns response with an error status.
)
])
Upvotes: 1