Permalink
Browse files

Step 10 of TODO App with React tutorial - using methods to do databas…

…e operations instead of client-side operations
  • Loading branch information...
itamaro committed Nov 28, 2015
1 parent 7f7c936 commit a4d831bdc25273b3521449de76d6aedfd254ac72
Showing with 27 additions and 12 deletions.
  1. +0 −1 .meteor/packages
  2. +0 −1 .meteor/versions
  3. +1 −6 App.jsx
  4. +2 −4 Task.jsx
  5. +24 −0 simple-todos-react.jsx
@@ -17,7 +17,6 @@ es5-shim # ECMAScript 5 compatibility for older browsers.
ecmascript # Enable ECMAScript2015+ syntax in app code

autopublish # Publish all data to the clients (for prototyping)
insecure # Allow all DB writes from clients (for prototyping)
react
accounts-ui
accounts-password
@@ -37,7 +37,6 @@ html-tools@1.0.5
htmljs@1.0.5
http@1.1.1
id-map@1.0.4
insecure@1.0.4
jquery@1.11.4
jsx@0.2.3
launch-screen@1.0.4
@@ -39,12 +39,7 @@ App = React.createClass({
// Find the text field via the React ref
var text = React.findDOMNode(this.refs.textInput).value.trim();

Tasks.insert({
text: text,
createdAt: new Date(), // current time
owner: Meteor.userId(), // _id of logged in user
username: Meteor.user().username // username of logged in user
});
Meteor.call("addTask", text);

// Clear form
React.findDOMNode(this.refs.textInput).value = "";
@@ -8,13 +8,11 @@ Task = React.createClass({

toggleChecked() {
// Set the checked property to the opposite of its current value
Tasks.update(this.props.task._id, {
$set: {checked: ! this.props.task.checked}
});
Meteor.call("setChecked", this.props.task._id, ! this.props.task.checked);
},

deleteThisTask() {
Tasks.remove(this.props.task._id);
Meteor.call("removeTask", this.props.task._id);
},

render() {
@@ -12,3 +12,27 @@ if (Meteor.isClient) {
React.render(<App />, document.getElementById("render-target"));
});
}

Meteor.methods({
addTask(text) {
// Make sure the user is logged in before inserting a task
if (! Meteor.userId()) {
throw new Meteor.Error("not-authorized");
}

Tasks.insert({
text: text,
createdAt: new Date(),
owner: Meteor.userId(),
username: Meteor.user().username
});
},

removeTask(taskId) {
Tasks.remove(taskId);
},

setChecked(taskId, setChecked) {
Tasks.update(taskId, { $set: { checked: setChecked} });
}
});

0 comments on commit a4d831b

Please sign in to comment.