javascript - React + Backbone application making 2 POSTS instead of 1 -
i have restful todo list implemented react , backbone.
for now, i'm not talking database; have array var mytodos = [];
in server.js.
here's server code post (using express):
app.post('/todos', function(req, res) { var todo = { id: mytodos.length, title: req.body.length, completed: req.body.completed }; mytodos.push(todo); console.log('post ' + json.stringify(todo)); res.send(todo); });
when add item list, see 2 posts being made reason.
post: {"id":0,"title":"test","completed":false} post: {"id":1,"title":"test","completed":false}
in chrome dev. mode, see:
put localhost/todos/0 404 (not found) put localhost/todos/1 404 (not found)
two put requests!
trying reason through - save()
called calls sync()
. since model doesn't have id
yet (only cid
), make post request , todo (id = 0) added mytodos
updates model on client (adds id
attribute). since change detected, save()
puts server (<-- may wrong here; not sure if updated model cause put).
this might explain put, not double post.
here save()
getting called (under todoapp = react.createclass...
:
componentdidupdate: function() { this.props.todos.foreach(function(todo) { console.log('componentdidupdate: ' + todo.id); // logs: undefined, 0, 1 todo.save(); }); }, ... save: function(todo, text) { console.log('save: ' + todo.id); // logged on update todo.save({title: text}); this.setstate({editing: null}); }, ... }
why happen?
why put
ting in addition post
ing? , why doing twice?
edit:
resolved double post changing create
add
- reference got help.
the last problem put 404 (not found)
item on server. interestingly, put
not going through server seems. server logs post
. put
being logged somewhere else.
i resolved double post changing create
add
more event triggering backbone.collection.create
call according this syncs server.
in react code, have componentdidupdate
calls backbone.model.save
tries sending server. here.
because event caused both create
, save
executed, post firing twice.
Comments
Post a Comment