如何从 coffeescript 调用 python

How to call python from coffeescript

我正在破解一个使用 coffeescript 创建的示例 angularjs 应用程序。我对这两个都是新手,但是 python.

有相当多的经验值

如何从 coffeescript 脚本中调用 flask?

具体来说,eg应用实例化了一个数组:

.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}
...
       ]
       ...

然后是 stores 数组上的一堆其他设置。而不是 coffeescript 文件中的这个静态列表,我想从这个脚本中调用一个 python 脚本(从数据库中获取行)。

任何帮助将不胜感激 - 例如我可以阅读的良好在线资源。我似乎找不到任何示例。

您想使用内置的 $http service to make the call to the server. For some sugar you have the option of using the $resource 服务,它包装了 $http 服务。理想情况下,您会在自己制作的服务中创建一个方法来从您的控制器进行调用..

我的控制器(注入了我的服务)

angular.module 'app'

.controller 'RegisterFormCtrl', (UsersService) ->
  UsersService.create(@details)
    .then()....

我的服务(注入了我的资源)

angular.module('app')

.factory 'UsersService', (UsersResource) ->
  new class
    constructor: ->
      @response = null

    # register
    create: (user) ->
      UsersResource.save(user).$promise

我的资源

angular.module('app')

.factory "UsersResource", (Resty) ->
  Resty "/api/users/:id", {id: '@id'}

.factory "UserKeysResource", ($resource) ->
  $resource "/api/userkeys"

我的资源实际上使用了我的另一个服务,该服务使用 angular $resource 服务..

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

好的,所以在您的控制器中,最低限度的替代实现如下..

.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.
    )
])