Last active
August 29, 2015 13:56
-
-
Save peterpme/9334424 to your computer and use it in GitHub Desktop.
Messing around with .map and .splice
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 imgLoader = { | |
config: { | |
container: '.imgContainer', | |
images : '#images', | |
imageLoadBtn: '.loadImages', | |
defaultSplice:4, | |
url : 'http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?' | |
}, | |
imgData: [], | |
init: function(config) { | |
$.extend(imgLoader.config, config); | |
}, | |
/* get data from json */ | |
loadData: function() { | |
$.getJSON(imgLoader.config.url, { | |
tags: "earthporn", | |
tagmode: "any", | |
format: "json" | |
}) | |
.done(function(data){ | |
imgLoader.imgData = data.items; | |
imgLoader.addData(imgLoader.config.defaultSplice); | |
}); | |
}, | |
/* add more data to array */ | |
addData: function(num) { | |
var push = imgLoader.imgData.splice(0,num || 1); | |
if(push.length > 0) { | |
console.log(push.length); | |
push.map(function(img){ | |
// | |
$('<img>').attr('src',img.media.m).appendTo(imgLoader.config.images); | |
}); | |
} | |
else { | |
$('.loadImages').addClass('disabled'); | |
} | |
}, | |
/* remove data from array */ | |
removeData: function(num) { | |
$('#images > img:last').remove(); | |
} | |
}; | |
$(document).ready(function() { | |
imgLoader.loadData(); | |
$('.loadImages').click(function(){ | |
imgLoader.addData(1); | |
}); | |
$('.removeImages').click(function() { | |
imgLoader.removeData(1); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment