[Answer]-Backbone fetching url and sending templateargs to javascript template

1👍

You cannot create the view without actually receiving the information from the server. You’re basically passing an empty Backbone model to the view controller, that’s why model.get('photo') is returning undefined. I’d recommend you to review basic AJAX, since that’s what Backbone.Model.fetch does.

Asynchronous calls return immediately to avoid freezing the user interaction. That’s the reason why you should not create the view until you do get the response from the server. Right solution will be something like this:

var photoItem = new PhotoItem({id:1}), photoView;
photoItem.fetch({
  success: function () {
    photoView = new PhotoView({model: photoItem});
  },
  error: function () {
    alert('Something bad happened!);
  }
});

Leave a comment