Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context of function after used with Meteor.bindEnvironment?

I have problem with this code recently:

function doSth() {
  console.log(this);
}

const fWithMeteorEnv = Meteor.bindEnvironment(doSth);

fWithMeteorEnv.call({});  // expect to see a plain object in console

What I expect is to see a plain object in console but not, it is something else. It seems that Meteor.bindEnvironment prevents the returned function to be called with another context. Are there any way to get around this?

like image 549
Huy Le Avatar asked Jan 14 '17 05:01

Huy Le


1 Answers

I think that what you're trying to achieve is not possible, i.e. you will need to bind your context at the moment you're calling Meteor.bindEnvironment. You can do it with .bind(), e.g.

const fWithMeteorEnv = Meteor.bindEnvironment(doSth.bind(context));

or you can pass the context as the third argument to Meteor.bindEnvironemnt(), e.g.

const fWithMeteorEnv = Meteor.bindEnvironment(doSth, null, context);

The second argument is an exception callback.

like image 131
Tomasz Lenarcik Avatar answered Sep 28 '22 05:09

Tomasz Lenarcik