Last active
August 30, 2017 21:59
-
-
Save zarkomilosevic/50554cb685e95dababc7685e5c8c1e8e to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Created by zarko on 7/2/17. | |
*/ | |
'use strict'; | |
import * as jQuery from 'jquery'; | |
import * as Immutable from 'immutable'; | |
import TodoActionTypes from './TodoActionTypes'; | |
import TodoDispatcher from './TodoDispatcher'; | |
import Todo from './Todo'; | |
const Actions = { | |
getTodos(){ | |
let todos = Immutable.OrderedMap(); | |
jQuery.ajax({ | |
async: false, | |
method: 'GET', | |
url: '/todos/get_all.json', | |
success: function(res) { | |
for(var i = 0; i < res.length; i++) | |
{ | |
const todo = res[i]; | |
todos = todos.set(todo.id, new Todo({ | |
id: todo.id, | |
text: todo.text, | |
complete: todo.complete | |
})); | |
} | |
}, | |
}); | |
return todos; | |
}, | |
addTodo(text) { | |
jQuery.ajax({ | |
method: 'POST', | |
beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', jQuery('meta[name="csrf-token"]').attr('content'))}, | |
data: { | |
todo: {text: text}, | |
}, | |
url: '/todos.json', | |
success: function(todo) { | |
TodoDispatcher.dispatch({ | |
type: TodoActionTypes.ADD_TODO, | |
todo, | |
}); | |
}, | |
error: function (res) { | |
TodoDispatcher.dispatch({ | |
type: TodoActionTypes.ADD_TODO_ERROR, | |
errors: res.responseJSON.errors, | |
}); | |
} | |
}); | |
}, | |
}; | |
export default Actions; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment