Ember getJSON.done () vs .then ()

I am working with Ember, and the following code gets JSON from api.php script and displays the results in a template. My question is why the script breaks when I change the getJSON function to use .done () instead of .then ()? I get the following error:

: Failed Error: Validation failed: Ember.CollectionView content must embed Ember.Array. You have passed the object of the object.

If I register a response.items object during a function, I get the same results in the console, so I'm curious how Ember interprets 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; }); } }); 
+4
source share
1 answer

Not sure if this will help or not, but in my Ember app, I used the following to capture 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 + "]")); } } }; }); } 
0
source

Source: https://habr.com/ru/post/1479397/


All Articles