Last active
August 29, 2015 14:17
-
-
Save chucknado/b30d153102370de4db35 to your computer and use it in GitHub Desktop.
Source code for Zendesk Apps tutorial at https://support.zendesk.com/hc/en-us/articles/203903346
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
return { | |
requests: { | |
taskPost: function(new_task_data) { | |
return { | |
url: 'https://app.asana.com/api/1.0/tasks', | |
headers: {"Authorization": "Basic " + btoa('{api_key}' + ":")}, | |
type: 'POST', | |
contentType: 'application/json', | |
data: JSON.stringify(new_task_data) | |
}; | |
} | |
}, | |
events: { | |
'taskPost.done': 'notifySuccess', | |
'taskPost.fail': 'notifyError', | |
'click #add-btn': 'validateForm', | |
'app.activated':'showForm' | |
}, | |
notifySuccess: function() { | |
services.notify('Successfully created a new task.'); | |
}, | |
notifyError: function() { | |
services.notify('Problem with the POST request.', 'error'); | |
}, | |
sendFormData: function() { | |
var new_task = { | |
data: { | |
assignee: "me", | |
workspace: this.$('#workspace').val(), | |
name: this.$('#name').val(), | |
notes: this.$('#notes').val() | |
} | |
}; | |
this.ajax('taskPost', new_task); | |
this.$('#task-form')[0].reset(); | |
services.notify('Task sent!'); | |
}, | |
validateForm: function(event) { | |
event.preventDefault(); | |
var name = this.$('#name')[0]; | |
if (name.value.length == 0) { | |
services.notify('Name can\'t be blank.', 'error'); | |
} else { // good to go | |
this.sendFormData(); | |
} | |
}, | |
showForm: function() { | |
this.switchTo('task_form'); | |
} | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment