Last active
December 16, 2015 03:49
-
-
Save semateos/5372647 to your computer and use it in GitHub Desktop.
router for editing a single mongo object in meteor js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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