转换为咖啡脚本后,angular provider会抛出错误

时间:2015-09-06 04:25:08

标签: javascript angularjs coffeescript ionic angularjs-provider

我分享了这个非常好的回购https://github.com/loicknuchel/ionic-starter

并开始将应用程序翻译为咖啡脚本。几乎一切都运行良好,但文件: https://github.com/loicknuchel/ionic-starter/blob/master/www/js/common/parse-utils.js

我转换成了这个:

angular.module 'app'

.provider 'ParseUtils', ->
  credentials =
    applicationId: null
    restApiKey: null

  this.initialize = (applicationId, restApiKey) ->
    credentials.applicationId = applicationId
    credentials.restApiKey = restApiKey

  this.$get = ($http, $q, CrudRestUtils, Utils) ->
    service =
      createCrud: createCrud
      createUserCrud: createUserCrud
      signup: signup
      login: login
      loginOAuth: loginOAuth
      passwordRecover: passwordRecover
      toGeoPoint: toGeoPoint
      toPointer: toPointer
      toDate: toDate
    parseUrl = 'https://api.parse.com/1'
    parseObjectKey = 'objectId'

    getParseData = (result) ->
      if result and result.data
        if !result.data[parseObjectKey] and result.data.results
          return result.data.results
        else
          return result.data


    parseHttpConfig = headers:
      'X-Parse-Application-Id': credentials.applicationId
      'X-Parse-REST-API-Key': credentials.restApiKey

    createCrud = (objectClass, _processBreforeSave, _useCache) ->
      endpointUrl = parseUrl + '/classes/' + objectClass
      service = CrudRestUtils.createCrud(endpointUrl, parseObjectKey, getParseData, _processBreforeSave, _useCache,
        parseHttpConfig)

      service.savePartial = (objectToSave, dataToUpdate) ->
        objectId = if typeof objectToSave == 'string' then objectToSave else objectToSave[parseObjectKey]
        toUpdate = angular.copy(dataToUpdate)
        toUpdate[parseObjectKey] = objectId
        service.save toUpdate

      service

    createUserCrud = (sessionToken, _processBreforeSave, _useCache) ->
      endpointUrl = parseUrl + '/users'
      parseUserHttpConfig = angular.copy(parseHttpConfig)
      parseUserHttpConfig.headers['X-Parse-Session-Token'] = sessionToken

      _processBreforeSaveReal = (user) ->
        delete user.emailVerified
        if _processBreforeSave
          _processBreforeSave user


      service = CrudRestUtils.createCrud(endpointUrl, parseObjectKey, getParseData, _processBreforeSaveReal, _useCache,
        parseUserHttpConfig)

      service.savePartial = (objectToSave, dataToUpdate) ->
        objectId = if typeof objectToSave == 'string' then objectToSave else objectToSave[parseObjectKey]
        toUpdate = angular.copy(dataToUpdate)
        toUpdate[parseObjectKey] = objectId
        service.save toUpdate

      return service

    # user MUST have fields 'username' and 'password'. The first one should be unique, application wise.

    signup = (user) ->
      if user and user.username and user.password
        $http.post(parseUrl + '/users', user, parseHttpConfig).then (result) ->
          newUser = angular.copy(user)
          delete newUser.password
          newUser.objectId = result.data.objectId
          newUser.sessionToken = result.data.sessionToken
          newUser
      else
        $q.reject data:
          error: 'user MUST have fields username & password !'

    login = (username, password) ->
      $http.get(parseUrl + '/login?username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password),
        parseHttpConfig).then (result) ->
          result.data

    # https://parse.com/docs/rest#users-linking

    loginOAuth = (authData) ->
      $http.post(parseUrl + '/users', {authData: authData}, parseHttpConfig).then (result) ->
        result.data

    passwordRecover = (email) ->
      $http.post(parseUrl + '/requestPasswordReset', {email: email}, parseHttpConfig).then ->
# return nothing


    toGeoPoint = (lat, lon) ->
      {
      __type: 'GeoPoint'
      latitude: lat
      longitude: lon
      }

    toPointer = (className, sourceObject) ->
      {
      __type: 'Pointer'
      className: className
      objectId: if typeof sourceObject == 'string' then sourceObject else sourceObject[parseObjectKey]
      }

    toDate = (date) ->
      d = Utils.toDate(date)
      if d
        return d.toISOString()
      throw 'Function toDate must be used with a timestamp or a Date object'

但是我只得到错误:

Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:pget] Provider 'ParseUtils' must define $get factory method.

但是我声明了$ get方法......

编辑:

这是代码的回购 https://github.com/bambamboole/ionic-bootstrap

这是在js编译的咖啡文件中:

angular.module('app').provider('ParseUtils', function() {
  var credentials;
  credentials = {
    applicationId: null,
    restApiKey: null
  };
  this.initialize = function(applicationId, restApiKey) {
    credentials.applicationId = applicationId;
    return credentials.restApiKey = restApiKey;
  };
  return this.$get = function($http, $q, CrudRestUtils, Utils) {
    var createCrud, createUserCrud, getParseData, login, loginOAuth, parseHttpConfig, parseObjectKey, parseUrl, passwordRecover, service, signup, toDate, toGeoPoint, toPointer;
    service = {
      createCrud: createCrud,
      createUserCrud: createUserCrud,
      signup: signup,
      login: login,
      loginOAuth: loginOAuth,
      passwordRecover: passwordRecover,
      toGeoPoint: toGeoPoint,
      toPointer: toPointer,
      toDate: toDate
    };
    parseUrl = 'https://api.parse.com/1';
    parseObjectKey = 'objectId';
    getParseData = function(result) {
      if (result && result.data) {
        if (!result.data[parseObjectKey] && result.data.results) {
          return result.data.results;
        } else {
          return result.data;
        }
      }
    };
    parseHttpConfig = {
      headers: {
        'X-Parse-Application-Id': credentials.applicationId,
        'X-Parse-REST-API-Key': credentials.restApiKey
      }
    };
    createCrud = function(objectClass, _processBreforeSave, _useCache) {
      var endpointUrl;
      endpointUrl = parseUrl + '/classes/' + objectClass;
      service = CrudRestUtils.createCrud(endpointUrl, parseObjectKey, getParseData, _processBreforeSave, _useCache, parseHttpConfig);
      service.savePartial = function(objectToSave, dataToUpdate) {
        var objectId, toUpdate;
        objectId = typeof objectToSave === 'string' ? objectToSave : objectToSave[parseObjectKey];
        toUpdate = angular.copy(dataToUpdate);
        toUpdate[parseObjectKey] = objectId;
        return service.save(toUpdate);
      };
      return service;
    };
    createUserCrud = function(sessionToken, _processBreforeSave, _useCache) {
      var _processBreforeSaveReal, endpointUrl, parseUserHttpConfig;
      endpointUrl = parseUrl + '/users';
      parseUserHttpConfig = angular.copy(parseHttpConfig);
      parseUserHttpConfig.headers['X-Parse-Session-Token'] = sessionToken;
      _processBreforeSaveReal = function(user) {
        delete user.emailVerified;
        if (_processBreforeSave) {
          return _processBreforeSave(user);
        }
      };
      service = CrudRestUtils.createCrud(endpointUrl, parseObjectKey, getParseData, _processBreforeSaveReal, _useCache, parseUserHttpConfig);
      service.savePartial = function(objectToSave, dataToUpdate) {
        var objectId, toUpdate;
        objectId = typeof objectToSave === 'string' ? objectToSave : objectToSave[parseObjectKey];
        toUpdate = angular.copy(dataToUpdate);
        toUpdate[parseObjectKey] = objectId;
        return service.save(toUpdate);
      };
      return service;
    };
    signup = function(user) {
      if (user && user.username && user.password) {
        return $http.post(parseUrl + '/users', user, parseHttpConfig).then(function(result) {
          var newUser;
          newUser = angular.copy(user);
          delete newUser.password;
          newUser.objectId = result.data.objectId;
          newUser.sessionToken = result.data.sessionToken;
          return newUser;
        });
      } else {
        return $q.reject({
          data: {
            error: 'user MUST have fields username & password !'
          }
        });
      }
    };
    login = function(username, password) {
      return $http.get(parseUrl + '/login?username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password), parseHttpConfig).then(function(result) {
        return result.data;
      });
    };
    loginOAuth = function(authData) {
      return $http.post(parseUrl + '/users', {
        authData: authData
      }, parseHttpConfig).then(function(result) {
        return result.data;
      });
    };
    passwordRecover = function(email) {
      return $http.post(parseUrl + '/requestPasswordReset', {
        email: email
      }, parseHttpConfig).then(function() {});
    };
    toGeoPoint = function(lat, lon) {
      return {
        __type: 'GeoPoint',
        latitude: lat,
        longitude: lon
      };
    };
    toPointer = function(className, sourceObject) {
      return {
        __type: 'Pointer',
        className: className,
        objectId: typeof sourceObject === 'string' ? sourceObject : sourceObject[parseObjectKey]
      };
    };
    return toDate = function(date) {
      var d;
      d = Utils.toDate(date);
      if (d) {
        return d.toISOString();
      }
      throw 'Function toDate must be used with a timestamp or a Date object';
    };
  };
});

1 个答案:

答案 0 :(得分:0)

Coffeescript解析器使用函数的最后一个句子作为函数的返回值,this.$get定义是函数的最后一个块,因此它被返回,所以它不是提供者功能的一部分。

您可以通过在功能块末尾添加return关键字来解决此问题:

this.$get = (...) ->
#inside the $get definition...
    toDate = (date) ->
      d = Utils.toDate(date)
      if d
        return d.toISOString()
      throw 'Function toDate must be used with a timestamp or a Date object'
    #still inside the $get function block
    return
#we're outside the $get function block, same indentation level...
return

现在你的代码将返回一个函数,错误将消失