meteor-astronomy icon indicating copy to clipboard operation
meteor-astronomy copied to clipboard

Events not firing

Open JulianKingman opened this issue 7 years ago • 4 comments

For some reason, the only events I can get to fire are the init events. Here's what my code looks like:

// in a server method
import {Team} from '../Teams';

const leagueTeams = Team.find({ Leagues: { $in: [league._id] } });
leagueTeams.forEach((team) => {
  //does a bunch of stuff
  team.save()
});

// in Teams.js
import { Class } from 'meteor/jagi:astronomy';
const Teams = Class.create({
  name: 'Team',
  collection: Teams,
  fields: {
    // a bunch of fields
  },
  events: {
    afterInit(e) {
      const team = e.currentTarget;
      console.log('init', team);
    },
    beforeSave(e) {
      console.log('before save', e.currentTarget);
    },
    beforeInsert() {
      console.log('inserted');
    },
    beforeUpdate() {
      console.log('updated');
    },
    afterSave() {
      console.log('after save');
    },
  },
}

export {Team};

The only one that fires is init. Any idea why? The collection is a scorpius collection with simple-schema attached (overkill I know), so perhaps that's related? I'm having a tough time implementing a calculated field.

JulianKingman avatar Mar 28 '17 22:03 JulianKingman

Ah, nevermind. I think you have some clever dif checker in there somewhere, save doesn't actually get called unless the document has changed. Is this a bug or a feature?

JulianKingman avatar Mar 28 '17 22:03 JulianKingman

It's a feature, you shouldn't invoke operation if it's not needed. So if there are no modifications then operation won't be executed. Btw. I see some bugs in your example code but I assume that you just made some typos pasting code.

lukejagodzinski avatar Mar 29 '17 11:03 lukejagodzinski

What did I miss?

JulianKingman avatar Mar 30 '17 14:03 JulianKingman

For example you're importing Team instead Teams collection:

import {Team} from '../Teams';

Later you create class Teams instead of Team:

const Teams = Class.create({
  name: 'Team',
  collection: Teams,

and you passed Teams to the collection property which is undefined in this example.

lukejagodzinski avatar Mar 30 '17 18:03 lukejagodzinski