var User = Backbone.Model.extend({ defaults: { api_token: null, name: null }, initialize: function(){ var session = new Session(); this.set('api_token', session.getApiKey()) }, get_users: function(tble_name){ var tble_name = tble_name; $.ajax({ type: 'POST', url: '/api/list_users', data: {api_token : this.get('api_token')}, success: function(resp){ var r = JSON.parse(resp) data = "" names = ""; count = r.length; $.each(r,function(index,value){ data += ""+value+""; names += value; if(index + 1 < count) names += ","; }) if(tble_name == 'ping') $('#pinguser').val(names); else $('tr.remove-tr-users').remove(); $('#users_table tr:last').after(data); }, error: function(resp){ if(resp.status == 422){ new App.Views.Index() } $('#ping-alert')[0].innerHTML = resp.responseText; $('#ping-alert').css('display','block'); } }) }, create_user: function(){ var login = $('#new-login').val(); var password = $('#new-password').val(); $.ajax({ type: 'POST', url: '/api/create_user', data: {api_token : this.get('api_token'), attributes : {login : login, password : password}}, success: function(resp){ router.navigate("users", true); }, error: function(resp){ if(resp.status == 422){ new App.Views.Index() } $('#newuser-alert')[0].innerHTML = resp.responseText; $('#newuser-alert').css('display','block'); } }) }, delete_user: function(){ if(confirm("Are you sure you want to delete this user?")){ $.ajax({ type: 'POST', url: '/api/user/delete_user', data: {api_token : this.get('api_token'), user_id : this.get('name')}, success: function(resp){ router.navigate("users", true); }, error: function(resp){ if(resp.status == 422){ new App.Views.Index() } $('#newuser-alert')[0].innerHTML = resp.responseText; $('#newuser-alert').css('display','block'); } }) } }, new_ping: function(){ $.ajax({ type: 'POST', url: '/api/source/list_sources', data: {api_token : this.get('api_token'), user_id : this.get('name')}, success: function(resp){ var r = JSON.parse(resp); source_list = ""; var count = r.length; $.each(r, function(index,value){ source_list += value; if(index + 1 < count) source_list += ","; }) $('input#sources').val(source_list); }, error: function(resp){ if(resp.status == 422){ new App.Views.Index() } $('#newuser-alert')[0].innerHTML = resp.responseText; $('#newuser-alert').css('display','block'); } }) } })