开发者

Using Tempo & JSON with the Dribbble API

I'm new to JSON & am having a hard time trying to get my head around it. I found a cool library called Tempo ( https://github.com/twigkit/tempo/ , http://twigkit.github.com/tempo/ ) that seems to make working with JSON much easier but I still cannot get i开发者_开发问答t to work on my simple example using Dribbble's API. Can anyone point me in the right direction?

I have created a fiddle at: http://jsfiddle.net/mrmartineau/2uDZK/ which can also be viewed more easily here: http://fiddle.jshell.net/mrmartineau/2uDZK/show/ . You will see that I have tried a number of different things, none of which work... Also, I would like to know how I control how many results are displayed on the page if my example were to work - Dribbble has documentation here: http://dribbble.com/api (I am using the get_shots_list example btw: http://dribbble.com/api#get_shots_list)

I think I have an inherent misunderstanding of how to handle a JSON feed (if that's what you call it) so I would appreciate someone setting me straight.

Many thanks in advance


Just a couple of minor things. This is how you would invoke Tempo with JavaScript (including adding a 'loading' icon'.

$(document).ready(function() {
    var dribbble = Tempo.prepare('dribbble').notify(function(event) {
        if (event.type === TempoEvent.Types.RENDER_STARTING || event.type === TempoEvent.Types.RENDER_COMPLETE) {
            $('div#dribbble').toggleClass('loading');
        }
    });
    dribbble.starting();
    $.getJSON("http://api.dribbble.com/shots/popular?callback=?", function(data) {
        dribbble.render(data);
    });
});

And here's a template showing dribbbles and player information. Notice that we render the entire JSON response (data), and use a nested template (data-template="shots") to render each shot. This allows you access things like the {{found}} variable.

<div id="dribbble">
    <h2>{{total}} found</h2>

    <ol id="dribbbles">
        <li data-template="shots">
            <img src="{{image_url}}" height="150" width="200" />
            <h3><a href="{{url}}">{{title}}</a> ({{views_count}})</h3>              

            <img src="{{player.avatar_url}}" class="player" height="30" width="30" />
            <p class="name">Created by <b>{{player.name}}</b>, {{player.likes_received_count}} likes.
            </div>
        </li>
    </ol>
</div>

You can see this (styled) in action here: http://twigkit.github.com/tempo/examples/dribbble

Good luck! Stefan


Looks like the problem is the same domain policy for post and get requests. There is a way around this by using jsonp, which the Dribbble api supports. Check out JSONP.


You should use JSONP for doing this, and Dribbble already provides JSONP compatibility by supporting ?callback=function convention.

I modified the code to make it work: http://jsfiddle.net/2uDZK/2/

There seems to be some other problem with your template though, so I'm just logging the data to browser console.


Use this simples Javascript Dribbble API library: Dribbble.js

You can see a example here: Dribbble API Example

Its simple tu use:

// get the most popular shots 
Dribbble.list( 'popular', function( response ){ 
   // write your code here 
});

Thanks, good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜