Skip to content

Instantly share code, notes, and snippets.

@Torsten85
Created May 17, 2014 15:35
Show Gist options
  • Save Torsten85/03a071019e2ef0cd4749 to your computer and use it in GitHub Desktop.
Save Torsten85/03a071019e2ef0cd4749 to your computer and use it in GitHub Desktop.
Click "Insert random name" twice and the click "Update first name" ... why is the first name not updated?!
<template name="name_list">
<ul>
{{#each names}}
<li>{{this.name}}</li>
{{/each}}
</ul>
<button class="insert">Insert random name</button>
<button class="update">Update first name</button>
</template>
<body>
{{> name_list}}
</body>
if (Meteor.isClient) {
// init with no name
Session.set('name', null);
// local collection without server backend
names = new Meteor.Collection('Names', {connection: null});
// Reruns when Session var "name" changes
Deps.autorun(function () {
var name = Session.get('name');
if (name) {
// If we have a name, insert it
names.insert({name: name});
}
});
Template.name_list.helpers({
names: function () {
// all names
return names.find({});
}
});
Template.name_list.events({
'click button.insert': function () {
Session.set('name', 'Name' + Date.now());
},
'click button.update': function () {
var firstNameId = names.findOne()._id;
console.log('Updating', firstNameId);
names.update(firstNameId, {name: 'Updated'});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment