Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

meteor

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?

like image 915
John Bliss Avatar asked Mar 16 '13 00:03

John Bliss


1 Answers

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.

like image 168
Tarang Avatar answered Sep 28 '22 05:09

Tarang