Skip to content

Commit

Permalink
Add reset button and save current page on server.
Browse files Browse the repository at this point in the history
  • Loading branch information
PxlBuzzard committed Feb 16, 2014
1 parent cc82405 commit 598be99
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions client/views/hello.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<h3>Meteor: Last.FM check</h3>
<input type="text" id="userName" value="{{userName}}"></input>
<input type="button" value="Fetch" id="fetchButton" />
<input type="button" value="Reset" id="resetButton" />

<div id="chart-graph"></div>

Expand Down
20 changes: 16 additions & 4 deletions client/views/hello.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var userName = "Pxl_Buzzard";
var page = 1;
var amount = 10;
var amount = 9;
var chart = null;
graphData = [];

Expand All @@ -10,16 +9,29 @@ Template.hello.events({
$('#fetchButton').attr('disabled', 'true').val('loading...');

userName = $('#userName').val();
Meteor.call('fetchFromService', userName, page, amount, function (err, respJson) {
Meteor.call('fetchFromService', userName, amount, function (err, respJson) {
if (err) {
console.log("error occured on receiving data on server. ", err);
} else if(respJson) {
page++;
Meteor.call('addSongs', respJson, userName, function (err, respJson) { });
}
$('#fetchButton').removeAttr('disabled').val('Fetch');
});
},

'click #resetButton': function (e) {
e.preventDefault();
$('#resetButton').attr('disabled', 'true').val('clearing...');

userName = $('#userName').val();
Meteor.call('resetSongs', userName, function (err, respJson) {
if (err) {
console.log("error occured on receiving data on server. ", err);
}
$('#resetButton').removeAttr('disabled').val('Reset');
});
}

});

Template.hello.recentTracks = function () {
Expand Down
5 changes: 4 additions & 1 deletion collections/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ Accounts = new Meteor.Collection('accounts');

Meteor.methods({
createNewAccount: function(json) {
//scrub the json
// scrub the json
delete json.tracks;

// string to int the page number
json.page = 1;

// save the meta
Accounts.insert(json);
},
Expand Down
17 changes: 15 additions & 2 deletions collections/songs.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
Songs = new Meteor.Collection('songs');

Meteor.methods({
fetchFromService: function(userName, page, amount) {
fetchFromService: function(userName, amount) {
if (amount > 200) amount = 200;
var page = 1;
if (Accounts.findOne({user: userName}))
page = Accounts.findOne({user: userName}).page;
console.log(page);
var url = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user="+userName+"&api_key="+Meteor.settings.lastfm_apikey+"&limit="+amount+"&page="+page+"&format=json";

var result = HTTP.get(url, {timeout:30000});
Expand All @@ -11,8 +15,12 @@ Meteor.methods({
console.log(respJson.recenttracks['@attr'].user);

// grab the metadata about page numbers and update database
if (!Accounts.findOne({user: respJson.recenttracks['@attr'].user}))
if (!Accounts.findOne({user: userName})) {
Meteor.call('createNewAccount', respJson.recenttracks['@attr']);
}

// inc the page counter
Accounts.update({user: userName}, {$inc: {page: 1}});

// travel down to track list
respJson = respJson.recenttracks.track;
Expand All @@ -37,5 +45,10 @@ Meteor.methods({
for(var i = 0; i < json.length; ++i) {
Songs.insert(json[i]);
}
},

resetSongs: function(userName) {
Songs.remove({});
Accounts.update({user: userName}, {page: 1});
}
});

0 comments on commit 598be99

Please sign in to comment.