Skip to content

Instantly share code, notes, and snippets.

@notakaos
Created March 25, 2016 19:20
Show Gist options
  • Save notakaos/eeb7d960038f1bcecf9d to your computer and use it in GitHub Desktop.
Save notakaos/eeb7d960038f1bcecf9d to your computer and use it in GitHub Desktop.
Meteor 1.3 + React Simple Memo Step 8-1
meteor remove insecure
meteor add aldeed:simple-schema
// imports/api/memos/memos.js
import { Mongo } from 'meteor/mongo';
class MemosCollection extends Mongo.Collection {
insert(doc, callback) {
doc.createdAt = doc.createdAt || new Date();
const result = super.insert(doc, callback);
return result;
}
}
export const Memos = new MemosCollection('Memos');
Memos.deny({
insert() { return true; },
update() { return true; },
remove() { return true; },
});
// for debug
if (Meteor.isDevelopment) {
global.collections = global.collections || {};
global.collections.Memos = Memos;
}
// imports/api/memos/methods.js
import { Meteor } from 'meteor/meteor';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import { Memos } from './memos';
Meteor.methods({
'Memos.insert'(content) {
new SimpleSchema({
content: { type: String }
}).validate({ content });
this.unblock();
Memos.insert({content});
},
'Memos.update'(memoId, content) {
new SimpleSchema({
memoId: { type: String },
content: { type: String }
}).validate({ memoId, content });
this.unblock();
Memos.update({_id: memoId}, {$set: {content}});
},
'Memos.remove'(memoId) {
new SimpleSchema({
memoId: { type: String }
}).validate({ memoId });
this.unblock();
Memos.remove(memoId);
}
});
// imports/ui/containers/AppContainer.js
import AppLayout from '../layouts/AppLayout';
import { Memos } from '../../api/memos/memos';
import { createContainer } from 'meteor/react-meteor-data';
import { Meteor } from 'meteor/meteor';
import '../../api/memos/methods';
const createMemo = content => {
Meteor.call('Memos.insert', content);
};
const removeMemo = memoId => {
Meteor.call('Memos.remove', memoId);
};
const updateMemoContent = (memoId, content) => {
Meteor.call('Memos.update', memoId, content);
};
export default createContainer(() => {
return {
memos: Memos.find({}, {sort: {createdAt: -1}}).fetch(),
createMemo,
removeMemo,
updateMemoContent,
};
}, AppLayout);
// server/main.js
import { Meteor } from 'meteor/meteor';
import { Memos } from '../imports/api/memos/memos';
import '../imports/api/memos/methods';
Meteor.startup(() => {
if (Memos.find().count() === 0) {
const data = [
{content: 'Memo 1'},
{content: 'Memo 2'},
{content: 'Memo 3'},
{content: 'Memo 4'},
{content: 'Memo 5'},
];
data.forEach(memo => Memos.insert(memo));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment