-2

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

4

1 回答 1

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

最后,您的服务器端 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"});
    }
});

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

于 2013-04-30T00:13:20.117 回答