Closed dnish closed 8 years ago
You then need show the events after the user starts the app, not the ones before. I wrote something similar here:
@ManuelDeLeon Just had an idea, as I remembered that you've changed onCreated behavior. What do you think about this?
Template.message.viewmodel({
message:null,
liked:false,
wasLiked:false,
onCreated() {
this.wasLiked(this.liked());
},
autorun: [
function() {
if(this.wasLiked() != this.liked() { console.log("Fire event..."); }
}
]
});
On the first test, it seems to work.
I'm sorry but this question is regarding the overall design and structure of your system so I'm not in a good position to give an opinion. I will say that your code has a smell to it (too imperative). It may be symptom of a larger architectural problem.
I would do something like
Template.message.viewmodel({
message:null,
likes: 0,
liked: function () { return this.likes() > 0 },
autorun: [
function() {
if(this.liked() { console.log("Fire event..."); }
}
]
});
@fvpDev But in this case, it would fire automatically on every page reload, because it inherits the likes from the collection document. So for example, you have a posting which has 4 likes - if the user visits your page, liked()
is automatically true and you fire an event (f.e. an animation) which you want to fire only if "a like" happened during the visitors session.
In my example above you copy the "start value" into another variable and check if this one has changed during the visitors session. If yes, the event fires.
Oh right, yeah I meant to l leave that onCreated func in there as well. My bad
For example, I want to fire an event if a user likes a message - but this should only happen, if it's happened during the runtime and not if the user reloads the page. So for example, I have this message VM which inherits from a collection:
I want to fire the event when the "liked" property changes from false to true, but it should not fire if it's true on the beginning. Normally I would do something like this:
...but this will also run on every page reload.