လောကမှာ ကတ်သီးကတ်သပ် ဖြစ်နေတာတွေ အများကြီးပါ။ တကယ်တမ်း ကျွန်တော်က ကပ်တာ မဟုတ်ပါဘူး။ စဉ်းစားကြည့်လေ၊ ပီကေက သူ့ဖာသာ မကပ်ပါဘူး။ သူ့ကို တစ်ခုခုက လာကပ်လို့ နေမှာပေါ့။

Json Widget အတြက္ Javascript ထည့္သြင္းျခင္း

12 July 2007

(၁) Setting မ်ား ျပင္ဆင္ျခင္း

Settings >> Site Feed မွ Allow Blog Feed အား Short အျဖစ္ ေျပာင္းလဲ သက္မွတ္ေပးရမည္။


Settings >> Archiving မွ Enable Post Pages? အား Yes အျဖစ္ ေျပာင္းလဲ သက္မွတ္ေပးရမည္။



အဆင့္ (၂) Javascript ထည့္သြင္းျခင္း

ေအာက္ပါ Code ကို </head> ၏ အေပၚနားတြင္ ထည့္ေပးပါ။

<script type='text/javascript'>
// Developed by Hoctro - All rights reserved 2007
// This credit must be included in all your derived usages.

// "cb" is intended to be a common library, where different widgets would
// utitlize the shared operations such as getTitle, getLink, etc. from a json object.
var cb = {

// search function requires these parameters:
// 1. query: a blogger address, such as "hoctro.blogspot.com",
// 2. type: type of return data, either "comments" or "posts",
// 3. start: the start-index parameter (where to start extracting data)
// 4. increment: the number of elements the json will get back. (the smaller value, the faster time to travel back)
// 5. func: the returned function the json object will feed.

search: function(query, type, start, increment, func) {
var script = document.createElement('script');
script.setAttribute('src', 'http://' + query + '/feeds/' + type + '/default?alt=json-in-script&start-index='
+ start + '&max-results=' + increment + '&callback=' + func + '&orderby=published');
script.setAttribute('type', 'text/javascript');
document.documentElement.firstChild.appendChild(script);
},

// searchLabel function return a result of posts w/ a label query
// it requires these parameters:
// 1. query: a blogger address, such as "hoctro.blogspot.com",
// 2. an array of labels
// 3. func: the returned function the json object will feed.
searchLabel: function(query, label, func) {
var script = document.createElement('script');
script.setAttribute('src', 'http://' + query + '/feeds/posts/default/-/' + encodeURIComponent(label) +
'?alt=json-in-script&callback=' + func + '&orderby=published');
script.setAttribute('type', 'text/javascript');
document.documentElement.firstChild.appendChild(script);
},

// getTotalResults needs the json object, and it'll return the total number of comments (or posts) of the blog.
getTotalResults: function(json) {
return json.feed.openSearch$totalResults.$t;
},

// getStartIndex gets the start index of a search inside an json object.
getStartIndex: function(json) {
return json.feed.openSearch$startIndex.$t;
},

// getLink return a href link if "name" matches the content inside "a" tags) of the link
getLink: function(entry, name) {
var alturl;

for (var k = 0; k < entry.link.length; k++) {
if (entry.link[k].rel == name)
alturl = entry.link[k].href;
}
return alturl;
},

// getTitle gets the title of the title of an entry of a json object.
getTitle: function(entry) {
return entry.title.$t;
},

// getContent gets the content inside an entry of a json object.
getContent: function(entry) {
return entry.content.$t;
},

// getCommentAuthor: gets the commenter name inside an entry of a json object.
getCommentAuthor: function(entry) {
return entry.author[0].name.$t;
},

// Given a json label search, this function return the decoded label.
getLabelFromURL: function(json) {
for (var l = 0; l < json.feed.link.length; l++) {
if (json.feed.link[l].rel == 'alternate') {
var raw = json.feed.link[l].href;
// The next two lines are borrowed from Ramani's Neo Template
// code. Thanks Ramani!
var label = raw.substr(raw.lastIndexOf('/')+1);
return decodeURIComponent(label);
}
}
},
txt : function (s) {
return s + " Widget by <a href='http://hoctro.blogspot.com" + "'>Hoctro</a>";
}
};
</script>

အထက္ပါ Code သည္ ဘံုသံုးႏုိင္ေသာ လုပ္ေဆာင္ခ်က္မ်ား ပါ၀င္ပါသည္။ Json Object ၏ ေခါင္းစဥ္ရွာျခင္း၊ လင့္ရွာျခင္း စေသာ လုပ္ေဆာင္ခ်က္မ်ားအား ေရးသားထားျခင္းျဖစ္သည္။ ထုိ႔ေၾကာင့္ ထုိ Code ကို ထည့္သြင္းၿပီးေနာက္ပိုင္း ဤ Json Object ကို အသံုးျပဳထားေသာ မည့္သည္ လုပ္ေဆာင္ခ်က္အတြက္မဆို တစ္ခါ ထည့္သြင္းထား႐ံုျဖင့္ ထပ္မံ ထည့္သြင္းစရာ မလိုေတာ့ပါ။