Note that there are some explanatory texts on larger screens.

plurals
  1. PODuplicate photos in instagram pagination
    primarykey
    data
    text
    <p>I'm trying to pull photos from specific tag. Found an awesome tutorial and I've managed to pull photos from Instagram with pagination.</p> <p>The problem I'm facing now is duplicate photos being displayed if it reaches to the end of the photos.</p> <p><strong>HTML Source</strong></p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script src='http://code.jquery.com/jquery-1.7.2.min.js' type='text/javascript' charset='utf-8'&gt;&lt;/script&gt; &lt;script src='javascripts/application.js' type='text/javascript' charset='utf-8'&gt;&lt;/script&gt; &lt;link rel='stylesheet' href='stylesheets/application.css' type='text/css' media='screen'&gt; &lt;title&gt;Photo Stream &lt;/title&gt; &lt;meta name="description" content="Search for instagram images online."&gt; &lt;meta name="author" content="Omar Sahyoun"&gt; &lt;/head&gt; &lt;body&gt; &lt;!--&lt;form id='search'&gt; &lt;button class="button" type="submit" id="search-button" dir="ltr" tabindex="2"&gt; &lt;span class="button-content"&gt;Search&lt;/span&gt; &lt;/button&gt; &lt;div class='search-wrap'&gt; &lt;input class='search-tag' type='text' tabindex='1' value='cats' /&gt; &lt;/div&gt; &lt;/form&gt;--&gt; &lt;h2 id="search"&gt;Photo Stream &lt;/h2&gt; &lt;div id='photos-wrap'&gt; &lt;/div&gt; &lt;div class='paginate'&gt; &lt;a class='button' style='display:none;' data-max-tag-id='' href='#'&gt;View More...&lt;/a&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>Javascript File</strong></p> <pre><code>// Add trim function support for IE7/IE8 if(typeof String.prototype.trim !== 'function') { String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); } } // Instantiate an empty object. var Instagram = {}; // Small object for holding important configuration data. Instagram.Config = { clientID: 'xxxx', apiHost: 'https://api.instagram.com' }; // Quick and dirty templating solution. Instagram.Template = {}; Instagram.Template.Views = { "photo": "&lt;div class='photo'&gt;" + "&lt;a href='{url}' target='_blank'&gt;&lt;img class='main' src='{photo}' width='250' height='250' style='display:none;' onload='Instagram.App.showPhoto(this);' /&gt;&lt;/a&gt;" + "&lt;span class='heart'&gt;&lt;strong&gt;{count}&lt;/strong&gt;&lt;/span&gt;&lt;span class='comment'&gt;&lt;strong&gt;{count2}&lt;/strong&gt;&lt;/span&gt;" + "&lt;span class='avatar'&gt;&lt;iframe src='//www.facebook.com/plugins/like.php?href={url}&amp;amp;send=false&amp;amp;layout=button_count&amp;amp;width=40&amp;amp;show_faces=true&amp;amp;action=like&amp;amp;colorscheme=light&amp;amp;font&amp;amp;height=21&amp;amp;' scrolling='no' frameborder='0' style='border:none; overflow:hidden; width:80px; height:21px;' allowTransparency='true'&gt;&lt;/iframe&gt;&lt;/span&gt;" + "&lt;/div&gt;" }; Instagram.Template.generate = function(template, data){ var re, resource; template = Instagram.Template.Views[template]; for(var attribute in data){ re = new RegExp("{" + attribute + "}","g"); template = template.replace(re, data[attribute]); } return template; }; // ************************ // ** Main Application Code // ************************ (function(){ function init(){ bindEventHandlers(); } function toTemplate(photo){ photo = { count: photo.likes.count, count2: photo.comments.count, avatar: photo.user.profile_picture, photo: photo.images.low_resolution.url, url: photo.link }; return Instagram.Template.generate('photo', photo); } function toScreen(photos){ var photos_html = ''; $('.paginate a').attr('data-max-tag-id', photos.pagination.next_max_id) .fadeIn(); $.each(photos.data, function(index, photo){ photos_html += toTemplate(photo); }); $('div#photos-wrap').append(photos_html); } function generateResource(tag){ var config = Instagram.Config, url; if(typeof tag === 'undefined'){ throw new Error("Resource requires a tag. Try searching for cats!"); } else { // Make sure tag is a string, trim any trailing/leading whitespace and take only the first // word, if there are multiple. tag = String(tag).trim().split(" ")[0]; } url = config.apiHost + "/v1/tags/" + tag + "/media/recent?callback=?&amp;count=10&amp;client_id=" + config.clientID; return function(max_id){ var next_page; if(typeof max_id === 'string' &amp;&amp; max_id.trim() !== '') { next_page = url + "&amp;max_id=" + max_id; } return next_page || url; }; } function paginate(max_id){ $.getJSON(generateUrl(tag), toScreen); } function search(tag){ resource = generateResource(tag); $('.paginate a').hide(); $('#photos-wrap *').remove(); fetchPhotos(); } function fetchPhotos(max_id){ $.getJSON(resource(max_id), toScreen); } function bindEventHandlers(){ $('body').on('click', '.paginate a.button', function(){ var tagID = $(this).attr('data-max-tag-id'); fetchPhotos(tagID); return false; }); // Bind an event handler to the `click` event on the form's button $('form#search button').click(function(){ // Extract the value of the search input text field. var tag = $('input.search-tag').val(); // Invoke `search`, passing `tag`. search(tag); // Stop event propagation. return false; }); } function showPhoto(p){ $(p).fadeIn(); } Instagram.App = { search: search, showPhoto: showPhoto, init: init }; })(); $(function(){ Instagram.App.init(); // Start with a search on cats; we all love cats. Instagram.App.search('hwplus'); }); </code></pre> <p>Please help me to find a way to disable the 'View More' button if photos have reached the end. And is there a way to add cache in JSON object and fetch variables from Javascript?</p> <p>Thanks and appreciate.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload