Isolated Posts to be non-reactive on post_edit.

The idea here is to isolate the call to findOne() so that it stops
being reactive once data is found. The reason being that server scoring
updates will 'change' the post, and meteor will overwrite the user's
input, even if preserve is set. 
Kind of like {{#constant}}, except it waits for data.
This commit is contained in:
Tom Coleman 2012-10-02 11:59:22 +10:00
parent 3ec5375fbd
commit 8cfbc7f9af
2 changed files with 34 additions and 15 deletions

View file

@ -2,20 +2,18 @@
<div class="grid submit"> <div class="grid submit">
{{#with post}} {{#with post}}
<form class="grid-block form-horizontal"> <form class="grid-block form-horizontal">
{{#constant}} <div class="control-group">
<div class="control-group"> <label class="control-label">Title</label>
<label class="control-label">Title</label> <div class="controls"><input id="title" type="text" value="{{headline}}" /></div>
<div class="controls"><input id="title" type="text" value="{{headline}}" /></div> </div>
</div> <div class="control-group">
<div class="control-group"> <label class="control-label">URL</label>
<label class="control-label">URL</label> <div class="controls"><input id="url" type="text" value="{{url}}" /></div>
<div class="controls"><input id="url" type="text" value="{{url}}" /></div> </div>
</div> <div class="control-group">
<div class="control-group"> <label class="control-label">Body</label>
<label class="control-label">Body</label> <div class="controls" id="editor"><textarea id="body" value="" class="input-xlarge">{{body}}</textarea></div>
<div class="controls" id="editor"><textarea id="body" value="" class="input-xlarge">{{body}}</textarea></div> </div>
</div>
{{/constant}}
<div class="form-actions"> <div class="form-actions">
<a class="delete-link" href="/posts/deleted">Delete Post</a> <a class="delete-link" href="/posts/deleted">Delete Post</a>
<input type="submit" value="Submit" /> <input type="submit" value="Submit" />

View file

@ -2,7 +2,28 @@
Template.post_edit.helpers({ Template.post_edit.helpers({
post: function(){ post: function(){
return Posts.findOne(Session.get('selectedPostId')); // The idea here is to isolate the call to findOne() so that it stops
// being reactive once data is found. The reason being that server scoring
// updates will 'change' the post, and meteor will overwrite the user's
// input, even if preserve is set.
// Kind of like {{#constant}}, except it waits for data.
//
// XXX: either figure out a different approach, or factor this out and
// use everywhere.
var outerContext = Meteor.deps.Context.current;
var innerContext = new Meteor.deps.Context;
var post;
innerContext.onInvalidate(function() {
// we don't need to send the invalidate through anymore if post is set
post || outerContext.invalidate();
});
innerContext.run(function() {
post = Posts.findOne(Session.get('selectedPostId'));
})
return post;
} }
}); });