Skip to content

Instantly share code, notes, and snippets.

@jimkang
Last active December 16, 2015 03:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimkang/5368594 to your computer and use it in GitHub Desktop.
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).
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