99热99这里只有精品6国产,亚洲中文字幕在线天天更新,在线观看亚洲精品国产福利片 ,久久久久综合网

歡迎加入QQ討論群258996829
麥子學院 頭像
蘋果6袋
6
麥子學院

meteor入門學習心得

發(fā)布時間:2016-08-18 23:34  回復:0  查看:2168   最后回復:2016-08-18 23:34  

最近因為工作的需要,開始學習meteor 跟Rails相比,還是有很大短板的。對于rails熟手,不需要用這個。對于nodejs背景的同學,挺值得用。

槽點1:


nodejs中的rails, 但是不如rails直觀,調(diào)試速度不如rails (例如,刷新一次頁面,居然需要一秒)。

槽點2:

{{# each}} 之后還要 {{/each}}  , 不習慣 {{}}, 還是更喜歡  <%= %>

槽點3: 從controller中傳遞變量到view, rails中直接 @var1 , @var2 就好了。但是在meteor中,你可以在body. helper里,也可以指定在其他的helper中 。 (這里有點兒復雜了。)

個人感覺,在web開發(fā)方面,還是不如rails. 但是值得學習里面的思想,比如socket

應該也是one page app. 但是沒有深入。

安裝

# mac, linux: curl https://install.meteor.com/ | sh

最好打開vpn. 否則會很慢。

hello world.

To get started fast:

 

  $ meteor create ~/hi_meteor

  $ cd ~/hi_meteor

  $ meteor

可以看出, meteor 就是一個node組件( meteor vs. node = rails vs ruby ) 使用了mongodb, 跑在了3000端口上。

[[[[[ /workspace/hi_meteor ]]]]]              

 

=> Started proxy.                             

=> Started MongoDB.                           

=> Started your app.                          

 

=> App running at: http://localhost:3000/

會新建一個文件夾和三個文件:hi_meteor.css hi_meteor.html hi_meteor.js

# html: <head>

  <title>hi_meteor</title></head>

<body>

  <h1>Welcome to Meteor!</h1>

 

  {{> hello}}</body>

<template name="hello">

  <button>Click Me</button>

  <p>You've pressed the button {{counter}} times.</p></template>

 

#  JS文件:

if (Meteor.isClient) {

  // counter starts at 0

  Session.setDefault('counter', 0);

 

  Template.hello.helpers({

    counter: function () {

      return Session.get('counter');

    }

  });

 

  Template.hello.events({

    'click button': function () {

      // increment the counter when button is clicked

      Session.set('counter', Session.get('counter') + 1);

    }

  });

}

 

if (Meteor.isServer) {

  Meteor.startup(function () {

    // code to run on server at startup

  });

}

可以看出, HTML中, <template> 是一個顯示內(nèi)容的模板, {{ > name }} 就顯示  name 模板。相當于rails中的partial ( 會渲染出一段 html 片段), 變量要放在  js文件中的 Template.hello.helpers。

{{ }} 則是表達式, 相當于 jsp, asp, rails中的 <%= %>

js代碼中,if (Meteor.isClient)   中的代碼,只針對于客戶端生效。   Meteor.isServer只針對于服務器端生效。  里面的代碼,跟ruby比起來,還是很麻煩的. 

template

內(nèi)容比較多,參考github的教程里面基本就是一個 erb 大全,包括: {{#each }}, {{#if }}, {{#with}}

操作數(shù)據(jù)庫 和 model

meteor中似乎沒有model 的概念。它可以在client和server中都操作mongodb (相信在 client中僅僅是發(fā)送ajax請求到server )  下面的代碼是查詢數(shù)據(jù)庫中的所有Task

Tasks = new Mongo.Collection("tasks");

 if (Meteor.isClient) {

  // This code only runs on the client

  Template.body.helpers({

    tasks: function () {

      return Tasks.find({});

    }

  });

}

操作表單,寫入數(shù)據(jù)庫

先新增一個表單,表單只有一個字段: book 的名字:把以下代碼放到html文件中:

<form class='my_book'>

      <input type='text' name='book' placeholder='type book title' />

    </form>

把以下代碼放到js文件中:放到  Meteor.isClient 中:

Template.body.events({

    'submit .my_book': function(event){

      event.preventDefault();

      var text = event.target.book.value;

      console.info('text: ' + text);

      Books.insert({

        title: text,

        createAt: new Date()

      })

      event.target.book.value='';

    }   

  })

可以看出, 需要獲取對應表單的值的話,需要使用:   'submit .表單id',  function 中,必須加上: event.preventDefault(),防止頁面刷新, 然后 (rails中獲取表單的 params[:book] )等同于:  event.target.book.value

相比之下,還是 rails中的更加直觀。

修改刪除,

也都基本一樣, 為相關的link 加上click event, 然后在對應的function中 Books.update, Books.remove(this._id)

發(fā)布:

可以一鍵發(fā)布。 基本跟 capistrano一樣。但是在我的機器上沒有用過。

有內(nèi)置的 用戶管理系統(tǒng)。

用處不大。 官方的東西不一定就是我們想要的。

 

 

原文來自:推酷

您還未登錄,請先登錄

熱門帖子

最新帖子

?