Created
May 17, 2014 15:35
-
-
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?!
This file contains hidden or 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
<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> |
This file contains hidden or 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
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