Skip to content
This repository has been archived by the owner on Feb 26, 2021. It is now read-only.

Commit

Permalink
Add callbacks to all methods, pending error handling strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Sashko Stubailo committed Nov 10, 2015
1 parent 0d8d548 commit 2133951
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
20 changes: 15 additions & 5 deletions packages/lists-show/lists-show.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Template.listsShow.onCreated(function() {
Lists.methods.updateName.call({
listId: this.data.list._id,
newName: this.$('[name=name]').val()
}, (err) => {
alert(err.error); // XXX i18n
});
};

Expand All @@ -43,11 +45,13 @@ Template.listsShow.onCreated(function() {
if (confirm(message)) {
Lists.methods.remove.call({
listId: list._id
}, (err) => {
// At this point, we have already redirected home as if the list was
// successfully deleted, but we should at least warn the user their list
// could not be deleted
alert(err.error); // XXX i18n!
});

// XXX should this be optimistic?
// We need some way of calling this only if the client-side validation
// passes
FlowRouter.go('home');
return true;
}
Expand All @@ -58,9 +62,13 @@ Template.listsShow.onCreated(function() {
this.toggleListPrivacy = () => {
const list = this.data.list;
if (list.userId) {
Lists.methods.makePublic.call({ listId: list._id });
Lists.methods.makePublic.call({ listId: list._id }, (err) => {
alert(err.error); // XXX i18n
});
} else {
Lists.methods.makePrivate.call({ listId: list._id });
Lists.methods.makePrivate.call({ listId: list._id }, (err) => {
alert(err.error); // XXX i18n
});
}
};
});
Expand Down Expand Up @@ -153,6 +161,8 @@ Template.listsShow.events({
Todos.methods.insert.call({
listId: this.list._id,
text: $input.val()
}, (err) => {
alert(err.error); // XXX i18n
});

$input.val('');
Expand Down
4 changes: 4 additions & 0 deletions packages/lists-show/todos-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Template.todosItem.events({
Todos.methods.updateText.call({
todoId: this._id,
newText: event.target.value
}, (err) => {
alert(err.error); // XXX i18n
});
}, 300),

Expand All @@ -61,6 +63,8 @@ Template.todosItem.events({
'mousedown .js-delete-item, click .js-delete-item'() {
Todos.methods.remove.call({
todoId: this._id
}, (err) => {
alert(err.error); // XXX i18n
});
}
});
6 changes: 6 additions & 0 deletions packages/method/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ Method = class Method {
}

call(args, callback) {
// Accept calling with just a callback
if (_.isFunction(args)) {
callback = args;
args = {};
}

const options = {
returnStubValue: true
};
Expand Down
13 changes: 9 additions & 4 deletions packages/todos-app/app-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,16 @@ Template.appBody.events({
},

'click .js-new-list'() {
const listId = Lists.methods.insert.call();

console.log(listId);
const listId = Lists.methods.insert.call((err) {
if (err) {
// At this point, we have already redirected to the new list page, but
// for some reason the list didn't get created. This should almost never
// happen, but it's good to handle it anyway.
FlowRouter.go('home');
alert('Could not create list.');
}
});

// Doesn't work since the above doesn't return anything
FlowRouter.go('listsShow', { _id: listId });
}
});

0 comments on commit 2133951

Please sign in to comment.