Last active
December 16, 2015 03:19
-
-
Save jimkang/5368594 to your computer and use it in GitHub Desktop.
Possible way of dealing with Meteor collections' insert() not calling the callback when offline. This is code modified from the todos example (http://meteor.com/examples/todos).
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
Template.todos.events(okCancelEvents( | |
'#new-todo', | |
{ | |
ok: function (text, evt) { | |
function doAfterInsert(error, _id) { | |
console.log("I was counting on this _id:", _id); | |
} | |
var connected = Meteor.status().connected; | |
var tag = Session.get('tag_filter'); | |
var newTodoParams = { | |
text: text, | |
list_id: Session.get('list_id'), | |
done: false, | |
timestamp: (new Date()).getTime(), | |
tags: tag ? [tag] : [] | |
}; | |
if (!connected) { | |
// Make up our own _id. | |
newTodoParams._id = Meteor.uuid(); | |
} | |
Todos.insert(newTodoParams, | |
// Don't pass a callback to insert if we're not connected, as it'll | |
// never get called. | |
connected ? doAfterInsert : null | |
); | |
if (!connected) { | |
// Call the callback directly. | |
doAfterInsert(null, newTodoParams._id); | |
} | |
evt.target.value = ''; | |
} | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment