使用流星将更新后的数据库值添加到textarea中

问题描述:

我是新来的流星和学习创建Web应用程序看例子。我想追加数据库的更新值到html textarea。 例如,如果我更新键“传感器”的值,传感器值需要附加到textarea。我怎样才能用流星来做到这一点?使用流星将更新后的数据库值添加到textarea中

您使用车把助手和绑定更改您的文本区域更新域:如

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}}); 
    } 
}); 

最后你需要在你的服务器端JS的东西:

//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"}); 
    } 
}); 

这就是它更新文本区域时的基本要点,并在模板中显示值。当它更新时,它也将反映所有查看页面的标签/每个人的更新。