Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MTAppListing で Data API で複数のブログから取得した記事を扱うサンプル
(function($){
// MTAppjQuery v1.9.0 以前のバージョンの場合は下記が必要
$.extend({
objectSort: function(array, key, order, type) {
order = (order === 'ascend') ? -1 : 1;
array.sort(function(obj1, obj2){
var v1 = obj1[key];
var v2 = obj2[key];
if (type === 'numeric') {
v1 = v1 - 0;
v2 = v2 - 0;
}
else if (type === 'string') {
v1 = '' + v1;
v2 = '' + v2;
}
if (v1 < v2) {
return 1 * order;
}
if (v1 > v2) {
return -1 * order;
}
return 0;
});
}
});
$.when(
// 事前に複数のブログから記事を取得します
$.ajax({
url: '<mt:Var name="config.CGIPath"><mt:Var name="config.DataAPIScript">/v3/sites/2/entries',
data: {
limit: 9999999,
fields: 'id,title,categories'
}
}),
$.ajax({
url: '<mt:Var name="config.CGIPath"><mt:Var name="config.DataAPIScript">/v3/sites/6/entries',
data: {
limit: 9999999,
fields: 'id,title,categories'
}
})
).done(function(data1, data2){
$('#keywords').MTAppListing({
url: '<mt:Var name="config.CGIPath"><mt:Var name="config.DataAPIScript">/v3/sites/5/entries',
data: {
limit: 9999999,
fields: 'id,title,categories'
},
jsontable: {
header: {
id: 'ID',
title: 'タイトル',
categories: 'カテゴリ'
},
headerOrder: ['id', 'title', 'categories'],
itemsRootKey: 'items',
listingTargetKey: 'id',
listingTargetEscape: false
},
cbProcessResponse: function(cb, response){
// 最後のブログから取得したデータと事前に取得しておいたデータを結合します
Array.prototype.push.apply(response.items, data1[0].items);
Array.prototype.push.apply(response.items, data2[0].items);
for (var i = 0, l = response.items.length; i < l; i++) {
var categories = response.items[i].categories;
if (categories.length > 0) {
response.items[i].categories = categories[0].label;
}
}
$.objectSort(response.items, 'id', 'descend', 'numeric');
return response;
}
});
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment