Instantly share code, notes, and snippets.

Embed
What would you like to do?
日本製BaaSであるappiaries(アピアリーズ)のAPIを利用するためのJavaScriptライブラリ appiary.1.0.1.jsの使用を前提としたスニペット。ユーザー認証機能使用。
document.addEventListener("DOMContentLoaded",function(){
var appiary = new Appiary('_sandbox','message','app3b76cc6ba7fbc88e521e0ed4ec','d3d5b7704c0cd02','nickname');
appiary.nickname = '';
var articles = document.querySelector('#articles');
var login = document.querySelector('#login');
var message = document.querySelector('#message');
if(appiary.store_token){
post_mes.style.display = 'block';
message.style.display = 'none';
}else{
login.setAttribute('href',appiary.oauth_url+'&redirect_uri='+location.href);
post_mes.style.display = 'none';
message.style.display = 'block';
}
var col = appiary.collection('sign');
col.callback = function(e){
var res = JSON.parse(e.target.responseText);
var objs = res._objs;
objs.reverse();
articles.innerHTML='';
for(var i=0;i<objs.length;i++){
var art = Appiary.create('article',{},articles);
var text = Appiary.create('div',{class:'text'},art);
Appiary.create('text',objs[i]['text'],text);
var prop = Appiary.create('div',{class:'prop'},art);
var user = Appiary.create('span',{class:'user'},prop);
Appiary.create('text',objs[i]['nickname']||objs[i]['_uby'],user);
var date = Appiary.create('span',{class:'date'},prop);
var uts = new Date(objs[i]['_uts']);
Appiary.create('text',uts.toLocaleString(),date);
}
}
col.error = function(e){
articles.innerHTML=e.toString();
}
col.render = function(){
col.x({path:'-',onload:col.callback,onerror:col.error});
}
col.post=function(){
var text = document.querySelector('#mes_input').value;
var data = {'text':text, 'nickname':appiary.nickname||'anonymous'};
col.x({method:'POST',data:data,onload:col.render,onerror:col.error});
}
document.querySelector('#mes_button').addEventListener('click',col.post,false);
col.render();
if(appiary.user_token){
appiary.show_nickname=function(e){
getAllResponseHeaders());
if(typeof e.target.responseText=='string'){
var res = JSON.parse(e.target.responseText);
console.log('[nickname]'+res.nickname)
appiary.nickname = res.nickname;
login.innerHTML = res.nickname;
}else{
console.log('[show_nickname.responseText] '+e.target.responseText);
}
}
appiary.getUser({'onload':appiary.show_nickname});
}
},false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment