mercredi 28 janvier 2015

Fetch images from API using Ember.JS

I´m getting into Ember.JS through Balint Erdi´s Rock & Roll with Ember.js , so far, so good....


Now I try to implement a new route in order to show band albums, including their covers, fetched from a public API.


I have this Album object:



App.Album = Ember.Object.extend({
title: "",
rating: 0,
band: "",
cover: null
});


I´m trying to fetch de cover artwork from the mediabrainz API. But the problem is:


How/When should I make the API calls?


When the object is being created? (despite user never ask for it)



App.Album.create({
title: "The Headless Cross",
rating: 5,
cover: function () {
Ember.$.getJSON(url).then(function (data) {
// return ...
});
}});


At controller level? (didn`t work for me)_



App.BandAlbumsController = Ember.ArrayController.extend({
albums: function () {
var albums = this.get('band').get('albums');
albums.forEach(function(item) {
if (item.get("cover") == null)
// API call using $.getJSON()...
});
return albums;
}
});


At route level? (didn´t work either... undefined is not a function)



App.BandAlbumsRoute = Ember.Route.extend({
model: function (params) {
var albums = this.modelFor('band').get('albums');
albums.forEach(function(item) {
if (item.get("cover") == null)
// API call using $.getJSON()...
});
return albums;
}
});


Could be cool if covers get fetched after user ask for the corresponding route...


Any ideas??


Many thanks in adv,


Ariel





Aucun commentaire:

Enregistrer un commentaire