Skip to content
This repository has been archived by the owner on Apr 2, 2021. It is now read-only.

Commit

Permalink
Step 4.2: Add event handler for form submit
Browse files Browse the repository at this point in the history
  • Loading branch information
Sashko Stubailo committed Jul 15, 2015
1 parent 8434ab7 commit 7a1ace1
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions simple-todos.js
Expand Up @@ -7,4 +7,23 @@ if (Meteor.isClient) {
return Tasks.find({});
}
});

Template.body.events({
"submit .new-task": function (event) {
// Prevent default browser form submit
event.preventDefault();

// Get value from form element
var text = event.target.text.value;

// Insert a task into the collection
Tasks.insert({
text: text,
createdAt: new Date() // current time
});

// Clear form
event.target.text.value = "";
}
});
}

2 comments on commit 7a1ace1

@groszmann
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

up to this step 4.2 I was able to easily follow your nice tutorial. But here I´m completely puzzled: is simple-todos.js a new file in this project? It has not been mentioned before. If I create a new file with the code above I get this in browser:

Your app is crashing. Here's the latest log:
/Users/hgg/.meteor/packages/meteor-tool/.1.1.10.1b51q9m++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:245
throw(ex);
^
Error: A method named '/tasks/insert' is already defined
at livedata_server.js:1542:15
at Function.
.each..forEach (packages/underscore/underscore.js:113:1)
at [object Object].
.extend.methods (livedata_server.js:1538:7)
at [object Object].Mongo.Collection.defineMutationMethods (packages/mongo/collection.js:927:1)
at new Mongo.Collection (packages/mongo/collection.js:218:1)
at simple-todos.js:1:9
at /Users/hgg/simple-todos-react/.meteor/local/build/programs/server/app/simple-todos.js:40:4
at /Users/hgg/simple-todos-react/.meteor/local/build/programs/server/boot.js:242:10
at Array.forEach (native)
at Function.
.each._.forEach (/Users/hgg/.meteor/packages/meteor-tool/.1.1.10.1b51q9m++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
Exited with code: 8
Your application is crashing. Waiting for file change.

If I just insert the code on green background into simple-todos-react.jsx nothing seems to change. The input-field is still there but the list below stays empty after filling in and hitting return.

Grateful for any help.

@tonyj4102
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to delete the following from the existing jss file first before adding the code form 4.2

return Tasks.find({});

}

});

}

Please sign in to comment.