Skip to content

Instantly share code, notes, and snippets.

@semateos
Last active December 16, 2015 03:49
Show Gist options
  • Save semateos/5372647 to your computer and use it in GitHub Desktop.
Save semateos/5372647 to your computer and use it in GitHub Desktop.
router for editing a single mongo object in meteor js
var Router = Backbone.Router.extend({
routes: {
"": "index",
"edit/:id": "edit",
"new": "new"
},
//homepage
index: function(){
},
//edit a thing
edit: function(id) {
var thing = Things.findOne({_id: id});
this.renderView(Template.editor, {thing: thing});
},
//new story
new: function(){
this.renderView(Template.editor);
},
renderView: function(template, vars){
$('#view').html(Meteor.render(function () {return template(vars);}));
}
});
router = new Router;
Meteor.startup(function () {
Backbone.history.start({pushState: true});
});
<head>
<title>thing</title>
</head>
<body>
<div id="view"></div>
</body>
<template name="editor">
<div id="editor" data-id="{{thing.id}}">
<input class="title" type="text" name="title" placeholder="Untitled Thing" value="{{thing.title}}"/>
</div>
</template>
Things = new Meteor.Collection("things");
Template.editor.events({
'change .title': function(event){
var id = $('#editor').data('id');
var new_title = event.target.value;
if(id){
Things.update(id, {$set: {title: new_title}})
}else{
Things.insert({title: new_title}, function(err, id){
router.navigate('edit/' + id, {trigger: true});
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment