我分叉了这个非常好的回购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/bambambole/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';
};
};
});