Skip to content

Instantly share code, notes, and snippets.

@zarkomilosevic
Last active August 30, 2017 21:59
Show Gist options
  • Save zarkomilosevic/50554cb685e95dababc7685e5c8c1e8e to your computer and use it in GitHub Desktop.
Save zarkomilosevic/50554cb685e95dababc7685e5c8c1e8e to your computer and use it in GitHub Desktop.
/**
* 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