使用meteor将数据库的更新值附加到textarea

时间:2013-04-29 09:52:33

标签: meteor

我是meteor的新手并且正在学习如何创建Web应用程序。我想将数据库的更新值附加到html textarea。 例如,如果我更新键“sensor”的值,则需要将传感器值附加到textarea。我怎么能用流星做到这一点?

1 个答案:

答案 0 :(得分:0)

您使用把手助手并将更改绑定到文本区域以更新字段:例如

HTML

<template name="hello">
    <textarea name="address">{{address}}</textarea>
</template

客户端js

//Your collection that stores your data
MyCollection = new Meteor.Collection("mycollection");

//Your handlebars helper to give some data to address
Template.hello.address = function() {
    var address = MyCollection.findOne({name:address});
    if(address) return address.value;
}

//Something to bind changes to your address to your collection
Template.hello.events({
    'change [name=address]' : function(e,cxt) {
        var address = MyCollection.findOne({name:address});
        MyCollection.update(address._id, {$set:{value:e.currentTarget.value}});
    }
});

最后,你的服务器端也需要一些东西:

//The same collection as on your client
MyCollection = new Meteor.Collection("mycollection");

//Insert an address on the first time if there is nothing in yet:
Meteor.startup(function() {
    if(!MyCollection.findOne({name:address})) {
        MyCollection.insert({name:address,value:"10 Downing St, London, United Kingdom"});
    }
});

它的基本要点是更改文本区域时更新文本区域,并在模板中显示值。更新后,它还会将更新反映到查看该页面的所有选项卡/所有人。