0
votes

In NodeJS I have an api that gives me the data for a todo :

app.get('/api/todo/:_id' function (req, res, next) {
        Todo.findOne({ _id: req.params._id}, '', function (err, todo) {
            res.send(todo);
        });
    });

Now in Backbone Marionette, I am have loading_view and then I fetch a Todo from Backbone Model using Manager.request("todo:entity", _id). Once the todo is fetch I overwrite this view with new todo_view

Manager.todoPanel.show(loading_view);
var fetching_todo = Manager.request("todo:entity", _id);
$.when(fetching_todo).done(function (todo) {
     var todo_view = new Manager.TodoView({
          model: todo
     });
 Manager.todoPanel.show(todo_view);

Now the problem is:

I want to render a view when there is no todo returned. I mean an empty Marionette ItemView. Or in case of BackboneJS what should I return from server for the Backbone to understand that an Empty view has to be returned?

In this case only loading view is shown because if we cannot find a Todo with an _id res.send doesnot send anything.

2

2 Answers

0
votes

It doesn't work in deferred because promise rejected wasn't handled. Insert error callback in todo.fetch () as

    error: function () {defer.reject ('error message')}  

Then handle the error callback in

    $.when (fetching_todo).done (function (){}).fail (function (){});
1
votes

Try with something like this:

app.get('/api/todo/:_id' function (req, res, next) {
    Todo.findOne({ _id: req.params._id}, '', function (err, todo) {
        if (err) return next(err);
        if (todo)
            res.send(todo);
        else
            res.send(404); //not found
    });
});

On the client side use .fail:

Manager.todoPanel.show(loading_view);
var fetching_todo = Manager.request("todo:entity", _id);
$.when(fetching_todo).done(function (todo) {
 var todo_view = new Manager.TodoView({
      model: todo
 });
Manager.todoPanel.show(todo_view);
}).fail(function (err) {
  //show a not found message.
});