summaryrefslogtreecommitdiff
path: root/users/app/assets/javascripts/users.js
diff options
context:
space:
mode:
authorazul <azul@riseup.net>2013-07-06 07:51:54 -0700
committerazul <azul@riseup.net>2013-07-06 07:51:54 -0700
commita18efa42ddc1cf8692d55f76ca3e92792913f40d (patch)
tree00527737a38bdafcd2e175bb6caf5e30b3360de1 /users/app/assets/javascripts/users.js
parentd03e82b4df5075f796f56fb9568992b0ba0d7c07 (diff)
parentdc98ad8c6445182d60b3f1909e0260ace6fbfca5 (diff)
Merge pull request #55 from elijh/feature/new-ui
Feature/new ui
Diffstat (limited to 'users/app/assets/javascripts/users.js')
-rw-r--r--users/app/assets/javascripts/users.js74
1 files changed, 74 insertions, 0 deletions
diff --git a/users/app/assets/javascripts/users.js b/users/app/assets/javascripts/users.js
new file mode 100644
index 0000000..1d05692
--- /dev/null
+++ b/users/app/assets/javascripts/users.js
@@ -0,0 +1,74 @@
+(function() {
+ //
+ // LOCAL FUNCTIONS
+ //
+
+ var poll_users, prevent_default, form_failed, form_passed;
+
+ prevent_default = function(event) {
+ return event.preventDefault();
+ };
+
+ poll_users = function(query, process) {
+ return $.get("/1/users.json", {
+ query: query
+ }).done(process);
+ };
+
+ //
+ // PUBLIC FUNCTIONS
+ //
+
+ srp.session = new srp.Session();
+
+ srp.signedUp = function() {
+ return srp.login();
+ };
+
+ srp.loggedIn = function() {
+ return window.location = '/';
+ };
+
+ srp.updated = function() {
+ return window.location = '/users/' + srp.session.id();
+ };
+
+ //
+ // if a json request returns an error, this function gets called and
+ // decorates the appropriate fields with the error messages.
+ //
+ srp.error = function(message) {
+ var element, error, field;
+ if ($.isPlainObject(message) && message.errors) {
+ for (field in message.errors) {
+ error = message.errors[field];
+ element = $('form input[name$="[' + field + ']"]');
+ if (!element) {
+ next;
+ }
+ element.trigger('element:validate:fail.ClientSideValidations', error).data('valid', false);
+ }
+ } else if (message.error) {
+ alert_message(message.error);
+ } else {
+ alert_message(JSON.stringify(message));
+ }
+ };
+
+ //
+ // INIT
+ //
+
+ $(document).ready(function() {
+ $('#new_user').submit(prevent_default);
+ $('#new_user').submit(srp.signup);
+ $('#new_session').submit(prevent_default);
+ $('#new_session').submit(srp.login);
+ $('#update_login_and_password').submit(prevent_default);
+ $('#update_login_and_password').submit(srp.update);
+ return $('#user-typeahead').typeahead({
+ source: poll_users
+ });
+ });
+
+}).call(this);