From 213cab048068451858781e9a6d0481eec7300e8c Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Wed, 26 Nov 2014 13:54:05 -0400 Subject: [PATCH] Make application model callback based --- lib/actions/app.coffee | 22 ++++++---------- lib/resin/models/application.coffee | 40 +++++++++++++++++++++-------- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/lib/actions/app.coffee b/lib/actions/app.coffee index a59d16fd..48ca54fe 100644 --- a/lib/actions/app.coffee +++ b/lib/actions/app.coffee @@ -21,14 +21,13 @@ exports.create = authHooks.failIfNotLoggedIn (name, program) -> # Maybe we should break or handle better? slugifiedType = resin.device.getDeviceSlug(type) - resin.models.application.create(name, slugifiedType).then -> - return callback() - .catch(callback) + resin.models.application.create(name, slugifiedType, callback) ], resin.errors.handle exports.list = authHooks.failIfNotLoggedIn -> - resin.models.application.getAll().then (applications) -> + resin.models.application.getAll (error, applications) -> + resin.errors.handle(error) if error? resin.log.out resin.ui.widgets.table.horizontal applications, (application) -> application.device_type = resin.device.getDisplayName(application.device_type) @@ -39,10 +38,9 @@ exports.list = authHooks.failIfNotLoggedIn -> return application , [ 'ID', 'Name', 'Device Type', 'Online Devices', 'All Devices' ] - .catch(resin.errors.handle) - exports.info = authHooks.failIfNotLoggedIn (id) -> - resin.models.application.get(id).then (application) -> + resin.models.application.get id, (error, application) -> + resin.errors.handle(error) if error? resin.log.out resin.ui.widgets.table.vertical application, (application) -> application.device_type = resin.device.getDisplayName(application.device_type) @@ -50,16 +48,12 @@ exports.info = authHooks.failIfNotLoggedIn (id) -> return application , [ 'ID', 'Name', 'Device Type', 'Git Repository', 'Commit' ] - .catch(resin.errors.handle) - exports.restart = authHooks.failIfNotLoggedIn (id) -> - # TODO: Move this URL to config - resin.server.post("/application/#{id}/restart", resin.errors.handle) + resin.models.application.restart id, (error) -> + resin.errors.handle(error) if error? exports.remove = authHooks.failIfNotLoggedIn (id, program) -> resin.ui.patterns.remove 'application', program.parent.yes, (callback) -> - resin.models.application.remove(id).then -> - return callback() - .catch(callback) + resin.models.application.remove(id, callback) , resin.errors.handle diff --git a/lib/resin/models/application.coffee b/lib/resin/models/application.coffee index 48e9ded0..504a2c2e 100644 --- a/lib/resin/models/application.coffee +++ b/lib/resin/models/application.coffee @@ -1,9 +1,9 @@ _ = require('lodash') -Promise = require('bluebird') canvas = require('./_canvas') errors = require('../errors/errors') +server = require('../server/server') -exports.getAll = -> +exports.getAll = (callback) -> return canvas.get resource: 'application' options: @@ -11,22 +11,28 @@ exports.getAll = -> expand: 'device' .then (applications) -> if _.isEmpty(applications) - return Promise.reject(new errors.NotAny('applications')) + return callback(new errors.NotAny('applications')) - return applications + return callback(null, applications) -exports.get = (id) -> + .catch (error) -> + return callback(error) + +exports.get = (id, callback) -> return canvas.get resource: 'application' id: id .then (application) -> if not application? - return Promise.reject(new errors.NotFound("application #{id}")) + return callback(new errors.NotFound("application #{id}")) - return application + return callback(null, application) -exports.create = (name, deviceType) -> + .catch (error) -> + return callback(error) + +exports.create = (name, deviceType, callback) -> return canvas.post resource: 'application' data: @@ -37,11 +43,23 @@ exports.create = (name, deviceType) -> id = res?.id if not id? - return Promise.reject(new errors.NotFound('created application id')) + return callback(new errors.NotFound('created application id')) - return id + return callback(null, id) -exports.remove = (id) -> + .catch (error) -> + return callback(error) + +exports.remove = (id, callback) -> return canvas.delete resource: 'application' id: id + .then -> + return callback() + .catch (error) -> + return callback(error) + +exports.restart = (id, callback) -> + + # TODO: Move this URL to config + server.post("/application/#{id}/restart", callback)