Skip to content

Instantly share code, notes, and snippets.

@akodkod
Last active August 29, 2015 14:13
Show Gist options
  • Save akodkod/5516c57c7c9c77107493 to your computer and use it in GitHub Desktop.
Save akodkod/5516c57c7c9c77107493 to your computer and use it in GitHub Desktop.
'use strict';
timeout = null;
angular.module 'wearedoge'
.config [
'$stateProvider', '$urlRouterProvider',
($stateProvider, $urlRouterProvider) ->
$stateProvider
.state('bounties', {
url: '/bounties',
resolve: {
bounties: [
'$state', 'API', '$q',
($state, API, $q) ->
q = $q.defer()
API.bounty.get (response) ->
q.resolve(response.payload);
, (errorResponse) ->
API.process.error errorResponse.data.errors if errorResponse.data.errors
q.reject()
return q.promise;
]
}
controller: 'BountiesPageCtrl',
templateUrl: '<%= asset_path('bounties/bounties.html') %>',
public: true
})
.state('bounties.one', {
url: '/{id:[0-9]{1,8}}',
resolve: {
bounty: [
'$stateParams', 'API', '$state', 'Noty', '$q',
($stateParams, API, $state, Noty, $q) ->
#$scope.timeout = null
deferred = $q.defer();
API.bounty.get { id: $stateParams.id }, (response) ->
deferred.resolve(response.payload)
, (errorResponse) ->
console.log(errorResponse)
timeout and clearTimeout(timeout)
timeout = setTimeout () ->
API.process.error errorResponse.data.errors if errorResponse.data.errors
$state.go('bounties');
deferred.reject()
, 1000
return deferred.promise;
]
responses: -> true
}
controller: 'BountiesOnePageCtrl',
templateUrl: '<%= asset_path('bounties/bounties.one.html') %>',
public: true
})
.state('bounties.one.edit', {
url: '/edit',
controller: 'BountiesOneEditPageCtrl',
templateUrl: '<%= asset_path('bounties/bounties.edit.html') %>',
})
.state('bounties.new', {
url: '/new',
controller: 'BountiesNewPageCtrl',
templateUrl: '<%= asset_path('bounties/bounties.new.html') %>',
})
return
]
# List of Bounties
.controller('BountiesPageCtrl', [
'$rootScope', '$scope', '$state', 'API', 'bounties', 'WS'
($rootScope, $scope, $state, API, bounties, WS) ->
$scope.bounties = bounties;
WS.bounty.on 'create', (obj) ->
bounty = obj.payload
console.log 'BountiesPageCtrl bounty-create', bounty
if _.isArray($scope.bounties)
$scope.bounties.push bounty
else
$scope.bounties = [ bounty ]
# Update Bounty
WS.bounty.on 'update', (obj) ->
bounty = obj.payload
console.log 'BountiesPageCtrl bounty-update', bounty
# firstly remove that object
$scope.bounties.rejectObject ( _obj ) ->
_obj.id is bounty.id
# then add it
if _.isArray($scope.bounties)
$scope.bounties.push bounty
else
$scope.bounties = [ bounty ]
# Remove Bounty
WS.bounty.on 'destroy', (obj) ->
bounty = obj.payload
console.log 'BountiesPageCtrl bounty-destroy', bounty
# remove that object
$scope.bounties.rejectObject ( _obj ) ->
_obj.id is bounty.id
])
# New Bounty
.controller('BountiesNewPageCtrl', [
'$rootScope', '$scope', '$state', 'API', 'Noty', 'bounties',
($rootScope, $scope, $state, API, Noty, bounties) ->
# temporary placeholder
$scope.bounty =
title: 'Test Bounty'
description: 'Test Bounty Description'
award: 100,
expires_at: 'Wed Apr 23 2014 22:50:00 GMT+0300 (EEST)'
$scope.location =
name: 'Kharkiv, Ukraine'
lat: 49.9935
lng: 36.2303
# onSubmit create bounty method.
$scope.createBounty = ->
Noty.info('Please wait..', 'We are creating your bounty.')
API.bounty.save { bounty: $scope.bounty, location: $scope.location }, (response) ->
# add to the /bounties list
bounties.push response.payload
# redirect to newly created bounty
$state.go('bounties.one', id: response.payload.id) if response.payload?.id
Noty.success('Created!', 'Your bounty are successfully created')
, (errorResponse) ->
# show form errors
$scope.errors = errorResponse.data?.errors
#API.process.error(errorResponse.data.errors) if errorResponse.data?.errors
])
# Bounty Individual Page
.controller('BountiesOnePageCtrl', [
'$stateParams', '$rootScope', '$scope', 'API', '$state', 'Noty', 'bounty', 'bounties', 'responses', 'WS',
($stateParams, $rootScope, $scope, API, $state, Noty, bounty, bounties, responses, WS) ->
$scope.loadingResponses = true
$rootScope.bounty = angular.copy(bounty)
# If we want to get responses showed at once view loaded
# $scope.bounty.responses = angular.copy(responses)
##########
# Interactive Responses
WS.response.on 'create', (obj) ->
response = obj.payload
console.log 'BountiesOnePageCtrl response-create', response
if response.bounty.id is $scope.bounty.id
if _.isArray($scope.bounty.responses)
$scope.bounty.responses.push response
else
$scope.bounty.responses = [ response ]
WS.response.on 'update', (obj) ->
response = obj.payload
console.log 'BountiesOnePageCtrl response-update', response
if response.bounty.id is $scope.bounty.id
# firstly remove that object
$scope.bounty.responses.rejectObject ( _obj ) ->
_obj.id is response.id
# then add it
if _.isArray($scope.bounty.responses)
$scope.bounty.responses.push response
else
$scope.bounty.responses = [ response ]
WS.response.on 'destroy', (obj) ->
response = obj.payload
console.log 'BountiesOnePageCtrl response-destroy', response
if response.bounty.id is $scope.bounty.id
# remove that object
$scope.bounty.responses.rejectObject ( _obj ) ->
_obj.id is response.id
API.bounty.responses { id: $stateParams.id }, (response) ->
$scope.loadingResponses = false
$scope.bounty.responses = response.payload
, (errorResponse) ->
$scope.loadingResponses = false
API.process.error errorResponse.data.errors if errorResponse.data.errors
console.log(errorResponse)
$scope.sortByStatus = (element) ->
toReturn = 3
switch (element.status)
when 'accepted'
toReturn = 1
when 'waiting'
toReturn = 2
when 'rejected'
toReturn = 3
return toReturn
])
# Edit Bounty Individual Page
# TODO: remove or refactor this
.controller('BountiesOneEditPageCtrl', [
'$rootScope', '$stateParams', '$scope', 'API', '$state', 'Noty', 'bounty', 'bounties',
($rootScope, $stateParams, $scope, API, $state, Noty, bounty, bounties) ->
map = marker = null
# I copy bounty object, to isolate scope while editing
$scope.bounty = angular.copy(bounty)
$scope.bounty.bounty_redactions_attributes = [new Object]
$scope.location = $scope.bounty.location
# onSubmit
$scope.editBounty = () ->
Noty.info('Please wait..', 'We are editing your bounty.')
API.bounty.update { id: $stateParams.id }, { bounty: $scope.bounty, location: $scope.location }, (response) ->
# extend bounty object to save relation
angular.extend(bounty, response.payload)
Noty.success("Done!")
$state.go('bounties.one', { id: $scope.bounty.id })
, (errorResponse) ->
API.process.error errorResponse.data.errors if errorResponse.data.errors
console.log(errorResponse)
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment