Search code examples
apiember.jsjsonptumblrrouter

EmberJS Route to 'single' getting JSONP


I'm having trouble with EmberJS to create a single view to posts based on the ID, but not the ID of the array, I actually have a ID that comes with the json I got from Tumblr API.

So the ID is something like '54930292'.

Next I try to use this ID to do another jsonp to get the post for this id, it works if you open the api and put the id, and actually if you open the single url with the ID on it, works too, the problem is:

When, on the front page for example, I click on a link to go to the single, it returns me nothing and raise a error.

But if you refresh the page you get the content.

Don't know how to fix and appreciate some help :(

I put online the code: http://tkrp.net/tumblr_test/


Solution

  • The error you were getting was because the SingleRoute was being generated as an ArrayController but the json response was not an Array.

    App.SingleController = Ember.ObjectController.extend({
    
    });
    

    Further note that the model hook is not fired when using linkTo and other helpers. This because Ember assumes that if you linked to a model, the model is assumed to be as specified, and it directly calls setupController with that model. In your case, you need to still load the individual post. I added the setupController to the route to do this.

    App.SingleRoute = Ember.Route.extend({
      model: function(params) {
        return App.TKRPTumblr.find(params.id);
      },
      setupController: function(controller, id) {
        App.TKRPTumblr.find(id)
        .then(function(data) {
          controller.set('content', data.response);    
        });
      }
    });
    

    I changed the single post template a bit to reflect how the json response. One final change I made was to directly return the $.ajax. Ember understands jQuery promises directly, so you don't need to do any parsing.

    Here is the updated jsbin.