實時程式時代 – Meteor
来源:互联网 发布:纯软件管理的 编辑:程序博客网 时间:2024/06/06 08:01
互聯網程式發展真的一日都嫌太長了,發展速度乃前所未見。數年前網頁界開始玩 AJAX,人們按一下按鈕,頁面不用重新載入就更新了。今天來到實時程式時代,連按下按鈕這個動作也節省了,Facebook、Gmail 等等程式我倒真是開了一整天也沒有按過重新整理鍵。
Meteor 最近開始火起來了,很多媒體開始報導它,到底這東西是什麼來的呢?Meteor 是一款用來開發網絡程式的框架,不過和以往的框架都有點不一樣,它最強大的功能是能夠做到 hot-push,把最新的 data 從 server side push 到 client side,而且不用編寫任何一行代碼就做到,可見其強大之處。
Meteor 的兩大特色:
一、純 Javascript
Meteor 是基於 Node.js 的開發框架,因此大家只需使用 Javascript 就可以編寫 server side 和 client side 的代碼(當然,CSS 和 HTML 另計)。但當然,你或者會考慮安全問題,如果 server side 的代碼被人看到了會不會很危險呢?Meteor 容許你把兩邊的代碼分開,那就不用怕了吧!
二、實時頁面更新
每當資料庫的內容被改變了之後,Meteor 都會自動幫你把改變了的內容 push 到 client side,而這個過程你甚至是一行代碼都不用寫,Meteor 能夠處理得到!
好了,大家都是 programmer,知道你們喜歡看代碼,就一起看看如何使用這個偉大的工具吧。
首先,如何安裝 Meteor?很簡單,就一句 command:
curl https:
//install
.meteor.com | sh
然後,怎樣建立新 Project?
meteor create dukeland
#if you want to create an example project
meteor create --example leaderboard
最後,怎樣運行?
cd
dukeland
meteor
開啟你的瀏覽器,輸入 http://localhost:3000/ 就可以看到你剛剛建立的 Project 了。
如果你想把 example project deploy 到 Meteor 的官方伺服器的話,你只需輸入:
meteor deploy server-name.meteor.com
Little big detail:Meteor 官網會自動產生新的網址讓開發人員 deploy 他們的程式
如圖中的 convincingly-unbelieable-leaderboard-test.meteor.com
好吧,動手做一個簡單的聊天室,首先做一個 template。
<
head
>
<
title
>dukechat</
title
>
</
head
>
<
body
>
<
h1
>dukechat</
h1
>
{{> content}}
{{> input}}
</
body
>
<
template
name
=
"content"
>
<
div
class
=
"content"
>
{{#each messages}}
{{> message}}
{{/each}}
</
div
>
</
template
>
<
template
name
=
"message"
>
<
div
class
=
"msg"
>
<
span
class
=
"author"
>{{author}}</
span
>
<
span
class
=
"msg"
>{{msg}}</
span
>
</
div
>
</
template
>
<
template
name
=
"input"
>
<
div
class
=
"input"
>
<
input
type
=
"text"
id
=
"author"
/>
<
input
type
=
"text"
id
=
"msg-input"
/>
<
input
type
=
"button"
value
=
"Send"
/>
</
div
>
</
template
>
Meteor 的 Template 語法很易看,{{> name}} 代表要使用一個名為 name 的 template,{{#each}} {{/each}} 包圍著的就會被 iterate,不太難明吧?
Javascript 又如何呢?
Messages =
new
Meteor.Collection(
"messages"
);
if
(Meteor.isClient) {
var
input, author;
Template.input.events({
"click input[type=button]"
:
function
(){
if
(input.value ==
""
){
return
;
}
Messages.insert({
author: author.value,
msg: input.value,
time: +
new
Date
});
input.value =
""
;
}
});
Template.input.rendered =
function
(){
input =
this
.find(
"#msg-input"
);
author =
this
.find(
"#author"
);
author.value =
"dukeland#"
+ parseInt(Math.random() * 1000);
}
Template.content.messages =
function
(){
return
Messages.find({}, {sort: [
"time"
,
"desc"
]});
};
}
if
(Meteor.isServer) {
Meteor.startup(
function
() {
// code to run on server at startup
});
}
首先建立一個 Meteor 的 Collection(底層是用 MongoDB 來實現的),名為 messages。
用 if (Meteor.isClient) 來判斷代碼是否在 client side 運行,判定 server side 則用 Meteor.isServer。
Template.input.rendered
=
function
(
)
{
input
=
this.find
(
"#msg-input"
)
;
author
=
this.find
(
"#author"
)
;
author.
value
=
"dukeland#"
+
parseInt
(
Math.random
(
)
*
1000
)
;
}
在 template 在 client side render 好之後,就從 DOM 裏拿取元素並儲存起來待會兒使用。
Template.input.events({
"click input[type=button]"
:
function
(){
if
(input.value ==
""
){
return
;
}
Messages.insert({
author: author.value,
msg: input.value,
time: +
new
Date
});
input.value =
""
;
}
});
按下 Send 按鈕之後就會往 Messages 這個 Meteor Collection 加入一個新的 object(你可以注意到我沒有刻意去寫 server side 的 Messages 還是 client side 的 Messages),然後清空 input 裏的數值。
就是這樣,簡單聊天室就完成了。難以置信吧!不用再處理 client side server side 的東西,不用再寫 XHR,就這麼短短幾十行就完成一個簡單聊天室,很好很強大!
官網在此,想知道更多的就去看看吧!
http://dukeland.hk/2013/03/25/age-of-real-time-application/
- 實時程式時代 – Meteor
- 現代程式庫的多重角色
- meteor
- meteor
- Meteor
- 修正VS2010剪貼程式碼時中文夾雜亂碼問題
- 你活在一個什麼樣的時代
- meteor.js
- 3905 - Meteor
- uva1398 - Meteor
- Uva1398 Meteor
- Meteor Shower
- 初识Meteor
- meteor安装
- Meteor Start
- 初识meteor
- Meteor 路由
- Meteor 会话
- 简单动态规划集(二)
- solr4的几种特色功能
- 数论之除法表达式
- 面试题
- 我的编程竞赛之路 ——中国大学生计算机编程第一人楼天城访谈
- 實時程式時代 – Meteor
- 三极管工作状态【Q稳定措施】与反馈
- 在PHP中开发XML应用程序之基础篇
- oracle实时查询消耗资源sql
- c++ 容器类 介绍
- XMU1456 水体
- nextLine 与hasnextline注意事项
- 警告:Block的Retain Cycle的解决方法
- 怎么在eclipse中导入一个maven项目