Is it possible to do 2 way data-binding on meteor

后端 未结 1 1960
小鲜肉
小鲜肉 2021-02-07 12:08

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 ch

相关标签:
1条回答
  • 2021-02-07 12:44

    You could use the template events binding

    e.g if you have

    html

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

    client 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

    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.

    0 讨论(0)
提交回复
热议问题