lundi 27 avril 2015

Returning response from an asynchoronous call not working in Emberjs Helper

I'm trying to return a response from an ajax call to the Ember js helper. An Asynchoronous call using Promise in Helper is returning [object Object].

console.log(data.thumbnail_url); is working but the callback is not returning the (<img...>)response. Here is JsFiddle.

App.IndexRoute = Ember.Route.extend({
  model: function() {
   // for simply showing the problem, I've put the instagram id
   // Actually I will parse the id from the post body
   return ['fA9uwTtkSN'];
  }
});

Ember.Handlebars.registerBoundHelper("format-inst", function(input){
 // Add this part
 if (typeof input == 'undefined'){return;}

 function foo (URL) {
   return $.ajax({
       url: URL,
       dataType: "jsonp"
  });
 }
     var URL = 'http://api.instagram.com/oembed?url=http://instagr.am/p/'+input;
     var r = foo(URL).done(function (data){
                console.log(data.thumbnail_url);
                return '<img src="'+data.thumbnail_url+'">';                        
     });
     return new Ember.Handlebars.SafeString(r);
 });

Here is the HandleBars Template:

<script type="text/x-handlebars">
<h2>Welcome to Ember.js</h2>

 {{outlet}}
</script>

<script type="text/x-handlebars" data-template-name="index">
 <ul>
 {{#each post in model}}
  <li>{{format-inst post}}</li>
 {{/each}}
 </ul>
</script>




Aucun commentaire:

Enregistrer un commentaire