wyc
wyc

Reputation: 55263

Why doesn't Meteor update the following input on input event?

I have a Document collection with forms that save on input:

document_form.html:

<template name="documentForm">
  <form class="form" role="form">
    <input name="title" type="text" value="{{title}}">
  </form>
</template>

document_form.js:

Template.documentForm.events({
  "input input": function(e) {
    e.preventDefault();

    currentDocumentId = this._id;

    setTimeout(function() {
      Documents.update(currentDocumentId, {
        $set: {
          title: $(e.target).find('[name=title]').val()
        }
      });
    }, 3000);
  }
});

As you can see the Document document should update three seconds after input.

What I see instead is my new input for three seconds and then the old one comes back (the old input is being reverted).

So, say, I have a Document called Untitled. I'll type in the input AnotherTitle and then after 3 seconds the input will revert to Untitled.

What am I doing wrong here?

Upvotes: 0

Views: 193

Answers (2)

lucascurti
lucascurti

Reputation: 86

I think the problem is that you shouldn't use find('[name=title]').
find() is used to get the descendants of an element. Here, you are targeting to the input field so it would be enough to use: title: $(e.currentTarget).val()

Hope you find it useful!

Upvotes: 1

Ethaan
Ethaan

Reputation: 11376

To be sure its not the allow/deny

   //client.js
    Template.documentForm.events({
      "input input": function(e) {
       e.preventDefault();
        currentDocumentId = this._id;
        setTimeout(function() {
        Meteor.call('updateCurrentDocumentId', {
        $set: {
          title: $(e.target).find('[name=title]').val()
        }
      });
    }, 3000);
  }
});

//server.js

    Meteor.methods({
      updateCurrentDocumentId:function(id,item){
           Documents.update(id,item);
        }
     })

With this we avoid, the allow/deny.

Upvotes: 1

Related Questions