今回の目的
実践的なBaaSの利用法を学ぼう!
やること
React.
準備するもの
- reactmilk-tutorialをローカルに持ってきてください。
- (4月30日以前に記事を読んだ方)
Firebaseのアカウントを作って、 appを1つ作成してください (MilkcocoaがMQTT対応でメンテナンス中のため)。 - (5月1日以降に記事を読んだ方)
Milkcocoaのアカウントを作って、 appを1つ作成してください。
コードを読むだけで理解できる方は以下を読み進める必要はありませんが、
手順
Reactの動作確認
gulpの準備
npm i gulp gulp-webserver
してください。
Reactの動作環境を作る
<script src="http://cdnjs.cloudflare.com/ajax/libs/react/0.13.1/react.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/react/0.13.1/JSXTransformer.js"></script>
<script src="jsx/main.jsx" type="text/jsx;harmony=true"></script>
react本体とjsxコンパイラはcdnでホスティングされているので、.jsx
ファイルを読み込んで、text/
を付けましょう。これで実行時にES6とJSXを利用可能な状態として実行時に処理してくれます。
Milkcocoaの動作確認
Milkcocoaはちょうどメンテナンス期間で5月1日まで新規アプリケーションの作成を停止しているので、
<script src="https://cdn.firebase.com/js/client/2.2.3/firebase.js"></script>
<script src="https://cdn.pubnub.com/pubnub-dev.js"></script>
<script src="lib/milkcocoa.js"></script>
<script src="lib/load-milkcocoa.js"></script>
PubNubとFirebaseを無理矢理ひっつけて作ったので、
すぐに本家Milkcocoaは復活するので、load-milkcocoa.
の記述を書き換えてあげましょう。
(function(global){
var milkcocoa = new MilkCocoa("psuedo-twitter",
"pub-c-3110c846-acee-4a93-b334-605c31524237",
"sub-c-8472c902-d950-11e4-895c-02ee2ddab7fe");
global.milkcocoa = milkcocoa;
return global;
}(window));
フィードのビューの実装
tweetが表示されるフィードについて、
基本的に描画・
(function(global){
var FEED = React.createClass({
render(){},
onInputKeyPress(e){},
saveTweet(tweet){},
observeTweet(){},
replyFilter(content){},
renderTweet($div){},
createTweetDiv(tweet){},
username(){},
onLogoutClick(){},
onClickNotifier(){},
observeNotifier(user){}
});
global.FEED = FEED;
return global;
}(window));
FEEDクラスをwindowオブジェクトに逃がしてあげて、
ログイン如何に合わせてランディングページを表示するか、
今回は、
React.
(function(){
milkcocoa.getCurrentUser( (err, user) => {
var isLoggedIn = (err == null);
if(isLoggedIn){
milkcocoa.dataStore("tweet").query({}).done(tweets=>{
milkcocoa.dataStore(`notifications/${user.password.email.split("@")[0]}`).query({}).done(notifications=>{
React.render(<FEED tweets={tweets} user={user}
notifications={notifications} />, document.body,function(){
this.observeTweet();
this.observeNotifier(user.password.email.split("@")[0]);
});
});
});
} else {
React.render(<LP />, document.body);
}
});
}());
ログイン後画面の根幹になるFEEDクラスのrender関数は以下のような実装になっています。
- 通知数の表示ボックス
- display:noneされているリプライ一覧
- tweet一覧
- ログアウトボタン
が描画されています。
一覧表示をする際に配列をmapしているあたりが少しややこしいかもしれませんね。
render(){
var tweets = this.props.tweets;
return (<div id="feed">
<div id="notifier"
onClick={this.onClickNotifier}>{this.props.notifications.length}</div>
<NOTIFICATIONS notifications={this.props.notifications}
username={this.username()} />
<h1>{this.username() + "'s feed"}</h1>
<input type="text" placeholder="Tweets here!"
onKeyPress={this.onInputKeyPress} />
<div id="tweet_list">
{tweets.map((tweet)=>{
return <div key={tweet.id} className="tweet">
<p>{tweet.user}</p>
<p>{tweet.content}</p>
</div>;
})}
</div>
<button onClick={this.onLogoutClick}>Logout</button>
</div>);
}
通知ビューの実装
NOTIFICATIONコンポーネントは非常にシンプルです。
役割は
- 描画
- 通知監視
- イベントリスナー
だけです。
(function(global){
var NOTIFICATIONS = React.createClass({
render(){},
observeNotification(){},
onNotificationClick(e){}
});
global.NOTIFICATIONS = NOTIFICATIONS;
return global;
}(window));
同期の実装
通知監視だけBaaS特有の部分であり、
observeNotification(){
var ds_notification =
milkcocoa.dataStore(`notifications/${this.props.username}`);
ds_notification.on("set", data=>{
var $li = document.createElement("li");
$li.innerHTML = data.value.content;
$li.setAttribute("id", data.id);
$li.addEventListener("click", this.onNotificationClick);
document.getElementById("notification_list").appendChild($li);
});
ds_notification.on("remove", data=>{
document.getElementById(data.id).remove();
});
}
要点だけ見ましょう。
ds_notification.on("set", data=>{
// li要素作成
});
ds_notification.on("remove", data=>{
// li要素削除
});
はい、
同様に、
observeNotifier(user){
var ds_notification = milkcocoa.dataStore(`notifications/${user}`);
var len = this.props.notifications.length;
var $notifier = document.getElementById("notifier");
ds_notification.on("set", data=>{
len++;
$notifier.innerHTML = len;
});
ds_notification.on("remove", data=>{
len--;
$notifier.innerHTML = len;
});
}
こちらも、
ツイートフィルタの実装
tweetの中に自分の名前があったら通知するということで、
replyFilter(content){
var called_name = "";
var base = content.split("@");
for(var i=base.length-1; i>=1;i--){
called_name = base[i].split(" ")[0];
}
return called_name;
}
データストアの設計
今回のデータストアの設計を解説すると、
- tweetデータストア
- notification/<user_
id>データストア
の2つになります。
tweetデータストアは全体のツイートが集まるものになります。notification/<user_
このように、
全体の動作確認
以上のソースコードをgulpでローカルサーバに置いてlocalhost:8000
にアクセスすると以下のような感じになります。
![図1 スクリーンショット 図1 スクリーンショット](/assets/images/dev/feature/01/milkcocoa-baas/0009/thumb/TH800_001.png)
是非一度アクセスして、
まとめ
BaaSを用いるとerb、
もちろん、