Created
May 29, 2010 14:43
-
-
Save ravicious/418310 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var LastFm = (function(username) { | |
| var username = username, | |
| status = [], | |
| song = null | |
| function init(username) { | |
| this.username = username; | |
| } | |
| function handleLastFmError() { | |
| this.status = [0, "Problem z last.fm"]; | |
| } | |
| function handleListeningError() { | |
| this.status = [2, "Niczego teraz nie słuchasz!"]; | |
| } | |
| function handleSuccess(argument) { | |
| this.song = argument.recenttracks.track[0].artist['#text'] + " " + argument.recenttracks.track[0].name; | |
| this.status = [1, this.song, Object.toQueryString({q:this.song})]; | |
| } | |
| function find() { | |
| var self = this; | |
| new Ajax.JSONRequest('http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user='+username+'&limit=2&api_key=917d76183f70ca5d5b5a24abc13e2531&format=json', { | |
| onSuccess: function(response){ | |
| self.delegate(response); | |
| console.log(self.status) | |
| }, | |
| onFailure: self.handleLastFmError | |
| });// koniec jsonrequest | |
| return this; | |
| } | |
| function delegate(response) { | |
| var obj = response.responseJSON; | |
| console.log(obj); | |
| if (obj.error == undefined) { | |
| if (obj.recenttracks.track[0]["@attr"] != null) { | |
| this.handleSuccess(obj); | |
| } else { // listening error - użytkownik niczego nie słucha | |
| this.handleListeningError(); | |
| } | |
| } else { | |
| this.handleLastFmError(); | |
| } | |
| } | |
| return { | |
| username: username, | |
| init: init, | |
| find: find, | |
| status: status, | |
| delegate: delegate, | |
| handleSuccess: handleSuccess, | |
| handleListeningError: handleListeningError, | |
| handleLastFmError: handleLastFmError | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Aż tak bardzo nie różni się od tego co napisalem na żywca wtedy ;-)
Jedna rzecz:
selfto taki trochę hak i nie zalecam używania tego nadmiernie przy dużej ilości obiektów.