Skip to content

Loading…

Correct 'this' reference in statics of model. #1194

Closed
wants to merge 1 commit into from

2 participants

@synaptiko

I have followed example of statics from docs:

animalSchema.statics.findByName = function (name, cb) {
  this.find({ name: new RegExp(name, 'i') }, cb);
}

But it seems that 'this' is global context. I think it would be possible to use Function.bind to fix it - see my commit.

Sorry that I don't know where I should write test for it, they are too chaotic for me :-)

@aheckmann

this is already the Model in a static. I'm not sure what this is fixing, especially without tests.

@synaptiko

Ok, sorry, I'll check it again. Maybe it's problem only in my application tests.

@synaptiko

I found it and it was my fault. It was caused by usage of express-resource module and following initialization:

app.resource('pages', PageController, {base: '/api/', load: Page.findOneByUrl});

The 'load' is called as a function, without context, so there was global one. I fixed it in my code.

Once again sorry for this.

@synaptiko synaptiko closed this
@aheckmann

ah cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 5, 2012
  1. @synaptiko
Showing with 1 addition and 1 deletion.
  1. +1 −1 lib/model.js
View
2 lib/model.js
@@ -1679,7 +1679,7 @@ Model.compile = function compile (name, schema, collectionName, connection, base
// apply statics
for (var i in schema.statics)
- model[i] = schema.statics[i];
+ model[i] = schema.statics[i].bind(model);
// apply named scopes
if (schema.namedScopes) schema.namedScopes.compile(model);
Something went wrong with that request. Please try again.