Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Explicit binding of functions/methods passed to template engine #69

Merged
merged 2 commits into from

4 participants

@robertklep

I ran into an issue trying to get the Swig template engine to work with Locomotive: using any of the *Path() functions from within templates failed with a type error: TypeError: Object #<Object> has no method 'urlFor'.

Not quite sure who's at fault, but the reason turns out to be that the *Path() functions weren't bound to the current controller instance (but to the global app object, I think), and thus were running in the wrong context.

This patch explicitly binds all functions passed to the template engine to the current controller, which has solved my problems.

@Bloutiouf

Logically I have the same error with Jade. There's also a topic on StackOverflow http://stackoverflow.com/questions/17507198/routing-helpers-in-jade-views-locomotive-framework .

I tested the patch for the locals and it works. The broken error handling is already fixed.

Jared it would be great to have feedback from you for this bugfix.

@drudge

@jaredhanson What do you think of this?

@jaredhanson jaredhanson merged commit f459b76 into from
@jaredhanson
Owner

Merged and published as locomotive v0.3.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 3, 2013
  1. @robertklep
Commits on Feb 5, 2013
  1. @robertklep
This page is out of date. Refresh to see the latest.
Showing with 8 additions and 2 deletions.
  1. +8 −2 lib/locomotive/controller.js
View
10 lib/locomotive/controller.js
@@ -110,7 +110,13 @@ Controller.prototype.render = function(template, options, fn) {
}
Object.keys(this).filter(localProperties).forEach(function(key) {
- self.__res.locals[key] = self[key];
+ var value = self[key];
+
+ // Make sure functions are always run in the current controllers` context.
+ if (value instanceof Function)
+ value = value.bind(self);
+
+ self.__res.locals[key] = value;
});
var fopts = this.__app._formats[fmt] || {}
@@ -450,7 +456,7 @@ Controller.prototype._invoke = function(action) {
self[action]();
return;
} catch (e) {
- return self.error(err);
+ return self.error(e);
}
}
Something went wrong with that request. Please try again.