javascript
Day 23:使用 TimelineJS 构建精美的时间轴
確定今天的主題費了我不少工夫,我開始打算學習brain,后來又去看了看Twitter Server,但是我最后決定學習TimelineJS。本文將介紹如何使用TimelineJS為一系列文章創建一個精美的時間軸。
TimelineJS是什么?
TimelineJS 是一個開源庫,可以幫助你創建精美、可交互的時間軸。它可以使用Google試算表或基于JSON的REST后端作為數據來源。它可以處理不同種類的內容,從多個來源獲取媒體文件,包括:
TimelineJS Demo
我今天構建的示例程序以時間軸的形式展示我的《30天學習30種新技術》系列文章。它部署在OpenShift上,你可以在此訪問。
用戶訪問應用的/時,將看到包括所有已發表文章的時間軸。這背后通過REST(/api/v1/stories)獲取文章信息。
GitHub倉庫
今天的示例應用代碼可以從GitHub獲取。
兩分鐘內運行
這里假設你已經安裝了OpenShift客戶端工具。請參閱OpenShift文檔獲取安裝信息。
我們將開始創建名為day23demo的示例應用。
rhc create-app day23demo tomcat-7 mongodb-2 --from-code=https://github.com/shekhargulati/day23-timelinejs-demo.git這會為我們創建一個應用容器——gear,然后設置公開的DNS,創建私有git倉庫,最后利用你的Github倉庫中的代碼來部署應用。應用可以通過http://day23demo-{domain-name}.rhcloud.com/訪問。用你自己的OpenShift域名替換{domain-name} (域名有時也被稱為命名空間)。
應用部署完成后,你可以使用curl來創建新文章:
curl -i -X POST -H "Content-Type: application/json" -d '{"url":"https://www.openshift.com/blogs/day-21-docker-the-missing-tutorial","startDate":"2013,11,18"}' http://day23demo-{domain-name}.rhcloud.com/api/v1/stories背后的秘密
這個應用包括兩部分——使用Spring框架構建的后端和用TimelineJS、jQuery構建的前端。在我昨天的文章中,我詳細介紹了如何使用Spring框架和MongoDB來構建一個REST后端。更多信息請參考day 22
TimelineJS使用的JSON格式如下:
{"timeline":{"headline":"30 Technologies in 30 Days -- By Shekhar Gulati","type":"default","text":"Learn 30 Technologies in 30 Days","startDate":"2013,10,29","date":[{"id":"528cb57de4b015e760ed06be","url":"https://www.openshift.com/blogs/day-1-bower-manage-your-client-side-dependencies","headline":"Day 1: Bower--Manage Your Client Side Dependencies","text":"<p>...</p>","startDate":"2013,10,29","asset":{"media":"https://www.openshift.com/sites/default/files/bower- logo.png"}},{"id":"528cb5bee4b015e760ed06bf","url":"https://www.openshift.com/blogs/day-2-angularjs-getting-my-head-around-angularjs","headline":"Day 2: AngularJS--Getting My Head Around AngularJS","text":"...","startDate":"2013,10,30","asset":{"media":"https://www.openshift.com/sites/default/files/angularjs-from-30k-feet.png"}}]} }id和url是可選的。
index.html指明應用的用戶接口。我們使用jQuery發起GET請求。GET請求獲取的信息交給TimelineJS在id為timeline的div中渲染。createStoryJS函數初始化新的時間軸。
<!DOCTYPE html> <html lang="en"><head><title>30 Technology in 30 Days Timeline</title><meta charset="utf-8"><meta name="description" content="30 Technology in 30 Days Timeline"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-touch-fullscreen" content="yes"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"><!-- Style--><style>html, body {height:100%;padding: 0px;margin: 0px;}</style><!-- HTML5 shim, for IE6-8 support of HTML elements--><!--[if lt IE 9]><script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script><![endif]--><script type="text/javascript" src="lib/jquery-min.js"></script><script type="text/javascript" src="js/storyjs-embed.js"></script><script>$(document).ready(function() {$.get('/api/v1/stories',function(result){createStoryJS({type: 'timeline',width: '100%',height: '600',source: result,embed_id: 'timeline',debug: true});});}); </script></head><body><div id="timeline"></div></body> </html>這是今天的內容。要繼續反饋哦~
原文 Day 23: TimelineJS--Build Beautiful Timelines
翻譯 SegmentFault
總結
以上是生活随笔為你收集整理的Day 23:使用 TimelineJS 构建精美的时间轴的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Day 22: 使用Spring、Mon
- 下一篇: Day 25: 联合Tornado、Mo