C:\Users\Administrator\Desktop>meteor create todo-app
創(chuàng)建成功后生成目錄結(jié)構(gòu)如下所示(看網(wǎng)絡情況,可能需要好幾分鐘才能完成):
要查看應用程序,需要運行的命令 meteor 應用程序,然后在瀏覽器中打開URL:http://localhost:3000
C:\Users\Administrator\Desktop\todo-app>meteor
取而代之默認的文件結(jié)構(gòu),我們將重構(gòu)它。讓我們創(chuàng)建 client 文件夾,并創(chuàng)建 todo-app.html, todo-app.css和todo-app.js。
創(chuàng)建項目時程序自動創(chuàng)建了 client 和 server 這兩個目錄,這里我們先要把 client 和 server 這兩個目錄中的文件內(nèi)容清空,接著再創(chuàng)建以下所需的文件,執(zhí)行如下命令:
C:\Users\Administrator\Desktop\todo-app\client>touch todo-app.html
C:\Users\Administrator\Desktop\todo-app\client>touch todo-app.js
C:\Users\Administrator\Desktop\todo-app\server>touch server.js
C:\Users\Administrator\Desktop\todo-app>mkdir server
C:\Users\Administrator\Desktop\todo-app\collections>touch task-collection.js
我們開發(fā)的第一個開發(fā)步驟是為應用程序創(chuàng)建HTML。我們需要輸入字段,來添加新的任務。任務將有刪除和檢查功能列表的形式。我們也將顯示或隱藏已完成的任務的功能。
<head> <title>Todo App</title> </head> <body> <h1>Todo List ({{incompleteCount}})</h1> <label class = "hide-completed"> <input type = "checkbox" checked = "{{hideCompleted}}" /> Hide Completed Tasks </label> <form class = "new-task"> <input type = "text" name = "text" placeholder = "Add new tasks" /> </form> <ul> {{#each tasks}} {{> task}} {{/each}} </ul> </body> <template name = "task"> <li class = "{{#if checked}}checked{{/if}}"> <button class = "delete">x</button> <input type = "checkbox" checked = "{{checked}}" class = "toggle-checked" /> <span>{{username}} - {{text}}</span> </li> </template>
Tasks = new Mongo.Collection("tasks");
我們將在服務器端定義應用程序的方法。這些方法將來自客戶端的調(diào)用。在這個文件中,我們還將發(fā)布數(shù)據(jù)庫查詢功能。
//Publishing tasks from the server... Meteor.publish("tasks", function () { return Tasks.find({}); }); //Methods for handling MongoDb Tasks collection data... Meteor.methods({ addTask: function (text) { Tasks.insert({ text: text, createdAt: new Date(), }); }, deleteTask: function (taskId) { var task = Tasks.findOne(taskId); Tasks.remove(taskId); }, setChecked: function (taskId, setChecked) { var task = Tasks.findOne(taskId); Tasks.update(taskId, { $set: { checked: setChecked} }); } });
這是主要的客戶端JavaScript文件。該文件也可以被重構(gòu),但我們會在這里編寫所有的客戶端代碼。首先,我們訂閱在服務器上發(fā)布的任務集合。然后,我們在創(chuàng)建助手能夠處理應用程序邏輯,最后我們定義調(diào)用來自服務器的方法事件。
// Subscribing to the published tasks Meteor.subscribe("tasks"); // Show/Hide functionality Template.body.helpers({ tasks: function () { if (Session.get("hideCompleted")) { // If hide completed is checked, filter tasks return Tasks.find({checked: {$ne: true}}, {sort: {createdAt: -1}}); } else { // Otherwise, return all of the tasks return Tasks.find({}, {sort: {createdAt: -1}}); } }, hideCompleted: function () { return Session.get("hideCompleted"); }, incompleteCount: function () { return Tasks.find({checked: {$ne: true}}).count(); } }); // Events for creating new tasks and Show/Hide functionality. Calling methods from the server Template.body.events({ "submit .new-task": function (event) { event.preventDefault(); var text = event.target.text.value; Meteor.call("addTask", text); event.target.text.value = ""; }, "change .hide-completed input": function (event) { Session.set("hideCompleted", event.target.checked); } }); // Events for Deleting and Check/Uncheck functionality Template.task.events({ "click .toggle-checked": function () { // Set the checked property to the opposite of its current value Meteor.call("setChecked", this._id, ! this.checked); }, "click .delete": function () { Meteor.call("deleteTask", this._id); } });
我們正在開發(fā)完成后,我們可以在命令提示符窗口中部署應用程序。應用程序的部署名稱是 my-first-todo-app。
C:\Users\Administrator\Desktop\todo-app>meteor deploy my-first-todo-app
C:\Users\Administrator\Desktop\todo-app>meteor add session