11

In a controller:

/*globals Ember*/

import { raw as icAjaxRaw } from 'ic-ajax';
...
    myData: function() {
        var promise = new Ember.RSVP.Promise(function (resolve, reject) {
            var req = icAjaxRaw({
                type: 'GET',
                url: server+'/api/mydata?callback=?',
                dataType: 'jsonp', //problematic
            });
            req.then(
                function(result) {
                    console.log('myData', result.response);
                    resolve(result.response);   
                },
                function(response) {            
                    console.error('myData', response.jqXHR.responseText, response);
                    reject(response);
                }
            );
        });
        return promise;
    }.property(),

... and in the template using that controller:

{{myData}}

This displays:

    {
    "_id": 101,
    "_subscribers": []
    }

Which looks like an intermediate object, not what the promise resolves to. I have a feeling that this might be related to something to do with the ember run loop, as mentioned here

How to get the template to display what is displayed in the console log?

Taryn
  • 242,637
  • 56
  • 362
  • 405
bguiz
  • 27,371
  • 47
  • 154
  • 243
  • Until there is a more substantial body of questions about the `ic-ajax` library, please don't create a tag for it. I removed it from your post for a reason. – Martijn Pieters Jul 14 '14 at 07:38
  • 2
    @MartijnPieters that's the one tag that is most important to this question though – bguiz Jul 14 '14 at 07:41
  • No, not really. No expert will follow that tag, not yet; no one will *find* your post by following a tag with just one or two questions attached to it. – Martijn Pieters Jul 14 '14 at 07:44
  • **Don't** just keep re-adding the tag. A roll-back war is not going to go anywhere. Please discuss such matters on [meta] instead. Your specific tag came under scrutiny *because* of a [meta post](http://meta.stackoverflow.com/questions/265510/when-exactly-are-tags-created). – Martijn Pieters Jul 14 '14 at 07:47
  • 1
    So what if there is only one question with this tag, that makes the tag no less valid. By preventing the first one from being created, you prevent subsequent ones too. – bguiz Jul 14 '14 at 07:54
  • Of course not. Build a body of questions first, *then* create the tag. – Martijn Pieters Jul 14 '14 at 07:58
  • @bguiz Please create a question on [Meta Stack Overflow](http://meta.stackoverflow.com/) to discuss creation of the tag `ic-ajax` which appears to be somewhat controversial. – Taryn Jul 14 '14 at 11:15

1 Answers1

0

You can't return a promise from a computed property.

Computed properties dont resolve promises, which means 'myData' is a promise not the value the promise resolves into. You should probably move this into the Route's model hooks. If that is not an option you can do something like this:

myData: {},

getMyData: function() {
  var self = this;
  var req = ic.ajax.raw({
    type: 'GET',
    url: 'http://ip.jsontest.com/?callback=?',
    dataType: 'jsonp'
  });
  req.then(
    function(result) {
      console.log('myData', result.response);
      self.set('myData', result.response);
    },
    function(response) {
      console.error('myData', response.jqXHR.responseText, response);
    }
  );
}.on('init')

Check this JSBin

LarsJK
  • 2,196
  • 18
  • 23