bgu*_*uiz 10 javascript ajax ember.js rsvp.js
在控制器中:
/*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(),
Run Code Online (Sandbox Code Playgroud)
...并在使用该控制器的模板中:
{{myData}}
Run Code Online (Sandbox Code Playgroud)
这显示:
{
"_id": 101,
"_subscribers": []
}
Run Code Online (Sandbox Code Playgroud)
这看起来像一个中间对象,而不是承诺解决的问题.我觉得这可能与ember run循环有关,如此处所述
如何让模板显示控制台日志中显示的内容?
您无法从计算属性返回承诺。
计算属性不解析承诺,这意味着“myData”是一个承诺,而不是承诺解析为的值。您可能应该将其移至路线的模型挂钩中。如果这不是一个选择,你可以这样做:
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')
Run Code Online (Sandbox Code Playgroud)
检查这个JSBin
| 归档时间: |
|
| 查看次数: |
695 次 |
| 最近记录: |