是否可以在流星上进行 2 路数据绑定 [英] Is it possible to do 2 way data-binding on meteor

查看:37
本文介绍了是否可以在流星上进行 2 路数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是流星的新手.我正在寻找一种在模型/集合到模板之间执行 2 路数据绑定的方法.我的理解是,当集合的内容发生变化时,模板会对此变化做出反应并自行更新.但是,如何在用户键入时自动收集,例如在文本框中?

I am new to meteor.. I am looking for a way to perform 2 way databinding between a model/collection to template. It is my understanding that when the contents of a collection change, the template reacts to this change and updates itself. However, how to automatically the collection when a user types, for example, in a textbox?

推荐答案

您可以使用 模板事件绑定

例如如果你有

html

<template name="home">
    <input type="text" name="text" value="{{text}}"/>
</template>

客户端js

Template.home.text = function() {
    return MyCollection.findOne({_id:"1"}).text;
}

Template.home.events({
    'change input[name=text]':function(event,context) {
        MyCollection.update(_id, {$set:{text:event.target.value}});
    }
});

这样它就会在文本框失去焦点/按下回车键后立即更新

So that will make it update as soon as the textbox loses focus/enter is pressed/etc

如果您想使用提交按钮 &对于更酷的东西,请查看 github 上的流星控制器分支简单的表单系统目前正在开发中,以减轻这一点.

If you want to use the submit button & for something a bit cooler have a look at the controllers branch of meteor on github for the easy forms system currently in the works to easen this up a bit.

这篇关于是否可以在流星上进行 2 路数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆