灰烬getJSON.done()VS。那么() [英] Ember getJSON.done() vs .then()
问题描述
我正在与灰烬工作和以下code从api.php脚本获取JSON和显示模板上的结果。我的问题是,为什么剧本打破当我改变了的getJSON函数中使用.done()代替。那么()?我收到以下错误:
:未捕获的错误:断言失败:一个Ember.CollectionView的内容必须实现Ember.Array。您通过[对象的对象。
如果我登录的response.items对象的函数中,我得到在控制台相同的结果,所以我很好奇如何灰烬是间preting这是不同的。
App.IndexRoute = Ember.Route.extend({
型号:功能(){
返回App.Item.all();
}
});App.Item = Ember.Object.extend();App.Item.reopenClass({
所有:功能(){
返回$ .getJSON(api.php)。然后(功能(响应){
变种项= []; response.items.forEach(函数(项目){
items.push(App.Item.create(项目));
});
返回的物品;
});
}
});
不知道这将帮助或没有,但我在灰烬应用程序,我用下面抓住一些JSON:
APP.getJSON =功能(URL){
返回新Ember.RSVP.Promise(函数(解析,拒绝){
VAR XHR =新XMLHtt prequest(); xhr.open('GET',URL);
xhr.onreadystatechange =处理程序;
xhr.responseType ='JSON';
xhr.setRequestHeader(接受,应用/ JSON');
xhr.send(); 功能处理器(){ 如果(this.readyState === this.DONE){
如果(this.status === 200){
typeof运算(this.response)===串?决心(JSON.parse(this.response)):解析(this.response);
}其他{
拒绝(新错误(的getJSON:[+ URL +]与状态失败:[+ this.status +]));
}
}
};
});
}
I am working with Ember and the following code gets the JSON from the api.php script and displays the results on the template. My question is on why the script breaks when I change the getJSON function to use .done() instead of .then()? I get the following error:
:Uncaught Error: assertion failed: an Ember.CollectionView's content must implement Ember.Array. You passed [object Object] .
If I log the response.items object during the function, I get the same results in the console, so I am curious how Ember is interpreting this differently.
App.IndexRoute = Ember.Route.extend({
model: function() {
return App.Item.all();
}
});
App.Item = Ember.Object.extend();
App.Item.reopenClass({
all: function() {
return $.getJSON("api.php").then(function(response) {
var items = [];
response.items.forEach( function (item) {
items.push( App.Item.create(item) );
});
return items;
});
}
});
Not sure if this will help or not, but in my Ember App, I used the following to grab some JSON:
APP.getJSON = function(url) {
return new Ember.RSVP.Promise(function(resolve, reject){
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onreadystatechange = handler;
xhr.responseType = 'json';
xhr.setRequestHeader('Accept', 'application/json');
xhr.send();
function handler() {
if (this.readyState === this.DONE) {
if (this.status === 200) {
typeof(this.response) === "string" ? resolve(JSON.parse(this.response)) : resolve(this.response);
} else {
reject(new Error("getJSON: [" + url + "] failed with status: [" + this.status + "]"));
}
}
};
});
}
这篇关于灰烬getJSON.done()VS。那么()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!