0

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.

1 Answer 1

1

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.
    )
])
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the code. I kind of get it, but I'm at a level more noob than required to fully understand. Can I put all these controllers in one file? I don't need it to look good at this stage, just function to extract data from flask.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.