Skip to content

Instantly share code, notes, and snippets.

@matthiasak
Created October 8, 2014 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthiasak/e949d07cec5371c8fab4 to your computer and use it in GitHub Desktop.
Save matthiasak/e949d07cec5371c8fab4 to your computer and use it in GitHub Desktop.

http://glacial-inlet-3098.herokuapp.com/

my biggest questions right now:

  1. how to get the right listing when you click on one of the cards on page 1. see line 48

Here's what you had originally:

$('body').on('click', '.card', function() {
    // addItemTmpl(event.target.listing);
    addItemTmpl(data.results[0]);
});

In jQuery, you can access the '.card' that was clicked with this in the callback function:

$('body').on('click', '.card', function() {
    this; // <--- some '.card' element
});

Now, with that, you will need to add a unique id for that etsyItem that populates that '.card' element. For example, one card element might have a new 'etsy-id' attribute:

<div class="cell card" etsy-id="..." style="background-image: url(https://img0.etsystatic.com/032/0/5963526/il_fullxfull.592724454_7ift.jpg)">
    <div class="title">
        Teddy Bear Cowl, Choose your size
    </div>
</div>

After clicking on the card, you can access the etsy-id attribute on this with:

$('body').on('click', '.card', function() {
    var etsyId = this.getAttribute('etsy-id'); // <--- some '.card' element
});

Then you can use etsyId to find and load information about a particular Etsy item.

  1. I am trying to access the url in getSellerInfo() (line 137) in order to pull more info about the shop owner. Since I can access “user_id” through my main listing results, i wrote a function getUserId(),to return the user id, and then pass it through getSellerInfo(). getUserId() will log the right info on line 176, but i was unsuccessful in getting it to return the right info to be passed through getSellerInfo(), or at least save it as a global variable. I am thinking there are some gaps in my understanding of how to use prototypes with global variables, which is why i thought it would be good to come in to TIY and discuss it.

I think the error actually is in how you are using getUserId().

I notice that in getSellerInfo() you have:

EtsyClient.prototype.getSellerInfo = function(userId) {
    var self = this;
    var URIs = [
        ...
        // this.getUserId();, //<------ problem area
        ...
    ];
    return $.getJSON(URIs.join('')).then(function(data) {
        ...
    });
}

Based on the structure of getUserId() you have:

EtsyClient.prototype.getUserId = function() {
    var self = this;
    ...
    return $.getJSON(URIs.join('')).then(function(data) {
        ...
        return data.results[0].user_id;---doesnt return a number //<-- actually you are right, it doesn't. Instead it pipes data through!!
        ...
    });
}

getUserId() returns a promise, thus you need to do:

EtsyClient.prototype.getSellerInfo = function(userId) {
    var self = this;
    return this.getUserId().then(function(user_id){
        var URIs = [
            ...
            user_id,
            ...
        ];
        return $.getJSON(URIs.join('')).then(function(data) {
            ...
        });
    })
    
}
  1. i designed a nice logo which i was only gonna show once onload, if you look at lines 72 and 93, i left notes on what i was trying to do.

You could always have a variable that keeps track of how many times addLogo() has been called:

var timesLogoShown = 0;
function addLogo() {
    if(timesLogoShown !== 0) return;
    timesLogoShown++;
    $.get('./templates/logo.tmpl').then(function(myTemplateHTML) {
        $('#logoLocal').append(myTemplateHTML);
        $('.logo').addClass('position1');
        // y no work??
        //can i make logo appear only on first onload to aid navigation back to homepage? (use cache or path.js)
        window.scrollTo(0, 500);
    });
}
  1. i still have that question about the checkboxes on my final project, but will be updating that repo soon.
@atroppe
Copy link

atroppe commented Oct 8, 2014

Thanx! I got #1 to work, and pushed the changes, would u be able to let me know if this was what you had in mind, or did i overcomplicate it? I wrote a new function to accomplish this task, lines 167-201.

Also, I can't figure out how to reuse functions when prototypes are involved. example/ addItemTmpl(), i rewrote on lines 189 and 94 because wasnt sure how to call a prototype fn within a prototype fn, if that's possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment