0

タグに基づいて、特定の tumblr 投稿を表示しようとしています。

苦労しています: 1. tumblr に複数のタグを照会する方法は? -- (同様の投稿: Tumblr の読み取り API で複数のタグを照会する方法は? ) -- しかし、それを自分のスクリプトにどのように適用すればよいでしょうか? 2. タグに基づいて、レンダリングされた投稿にクラスを割り当てます。たとえば、投稿に「featured_1」というタグが付けられている場合、レンダリングされた html で「featured_1」のクラスを表示する必要があります。

よろしくお願いします!どんな助けでも感謝します。

ここに私が取り組んでいるスクリプトがあります:

/*
--------------------------------------
    Created by james <at> bandit.co.nz
    http://blog.bandit.co.nz
*/
Featured = {
    'apiNum' : 100, // how many posts to read
    'listId' : 'featured', // the id of the ul to write to
    'tagName' : 'featured', // the name of the tag we're searching for
    'tagName2': 'featured_1',
    'tagName3': 'featured_2',
    'tagName4': 'featured_3',

    'postDB' : [],
    'listPos' : 0,
    'doList' : function (where) {
        var li; var ul = $('#'+where);
        var titles = {"link":"link-text", "photo":"photo-caption", "quote":"quote-text", "regular":"regular-body", "video":"video-caption"}



        // cycle through post database
        pcount = Featured.postDB.length;
        for(i=Featured.listPos;i<pcount;i++) {
            p = Featured.postDB[i];
            if(p[titles[p.type]] != '') titlestr = p[titles[p.type]].replace(/<\/?[^>]+>/gi, '');
            else titlestr = p['url'];

            li = document.createElement('li');
            $(li).html('<p class=" ">'+titlestr+'</p>');
            ul.append(li);

            Featured.listPos = pcount;
        }
    },

    'getData' : function() {
        $.get('/api/read/json?num='+Featured.apiNum+'&tagged='+Featured.tagName3+Featured.tagName+Featured.tagName2,
            function(data) {
                eval(data);
                for(i=0;i<tumblr_api_read.posts.length;i++) {
                    Featured.postDB.push(tumblr_api_read.posts[i]);
                    Featured.doList(Featured.listId);
                }

            }
        );
    }
};

$(document).ready(function(){
    Featured.getData();
});
4

1 に答える 1

0

あなたのコード、またはあなたが提供したSO投稿のいずれかで、すべてがすでにそこにあります。複数のタグを取得する方法はないように思われるため (私は Tumbl ユーザーではありません)、各タグを個別にクエリする必要があります。(テストされていないコード。欠落{やタイプミスをどこかで修正する可能性があります)。

関数を変更しますgetData。現在、単一のタグのみを読み取り、結果を配列にプッシュする jqXHR を返します。

 'getData' : function(tag) {
       return $.get('/api/read/json?num=' + Featured.apiNum + '&tagged=' + tag,
       function(data) {
          //do NOT use eval or a kitten will die somewhere
          Featured.postDB.push(data.posts); //change according to the result
       });
    }

新しい関数を追加します (提供した投稿の Gary Green からコピー)。これにより、必要な各タグで以前の関数が呼び出されます。これらすべてが完了したら (when-then 関数に注意してください)、必要なことは何でも実行します。

'myFunction' : function() {
    $.when(Featured.getData(Featured.tagName1), Featured.getData(Featured.tagName2) /*, and so on */)
      .then(function() {
         var posts = $.extend({}, arguments); //you might not need this
         Featured.doList(Featured.listId);
      });
 }
于 2012-07-26T12:42:06.667 に答える