Skip to content

Instantly share code, notes, and snippets.

@nalply
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nalply/8733636 to your computer and use it in GitHub Desktop.
Save nalply/8733636 to your computer and use it in GitHub Desktop.
This is a minimal Meteor app. You go to an option by its link. Or use the select. It's the same. See: https://groups.google.com/forum/#!topic/meteor-talk/b1BAUN50zQg
<template name="index">
{{#each list}}<a href="{{pathFor 'select'}}">{{text}}</a><br>{{/each}}
</template>
<template name="select">
<a href="/">Home</a> -
<a href="/ex2">Select ex2</a>
<br><br>
<select>{{#each list}}
<option value="{{id}}" {{#if selected id}}selected{{/if}}>{{text}}</option>
{{/each}}</select>
<br><br>
currentText: <code>{{currentText}}</code>
</template>
Router.map(function() {
var rerun = false
this.route('index', { path: '/' })
this.route('select', { path: '/:id',
before: function() {
var id = Session.get("current")
if (rerun) {
var path = Router.path('select', {id: id})
if (id && this.params.id != id) this.redirect(path)
}
else Session.set("current", this.params.id)
},
unload: function() { rerun = false },
after: function() { rerun = true },
})
})
var list = [
{id: "ex1", text: "First option"},
{id: "ex2", text: "Second option"},
{id: "other", text: "Other option"},
]
Template.index.list = list
Template.select.list = list
Template.select.currentText = function() {
var id = Session.get("current")
var record = _.find(list, function(elem) { return elem.id == id })
return record && record.text
}
Template.select.selected = function(id) {
return id == Session.get("current")
}
Template.select.events({
'change select': function(e) {
Session.set("current", $(e.target).val())
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment