Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Bind handler #81

Open
wants to merge 2 commits into from

1 participant

@sgronblo

Redid this pull request with an included test case and verified all test cases still pass.

sgronblo added some commits
@sgronblo sgronblo Require should so tests can be run using mocha 03e891f
@sgronblo sgronblo Bind handler if it is a function
- This will be properly bound if it is used in the handler
- Not supported for the case where the handler is an object which maps
  format name strings to handler functions
a30372b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 15, 2013
  1. @sgronblo
  2. @sgronblo

    Bind handler if it is a function

    sgronblo authored
    - This will be properly bound if it is used in the handler
    - Not supported for the case where the handler is an object which maps
      format name strings to handler functions
This page is out of date. Refresh to see the latest.
Showing with 19 additions and 1 deletion.
  1. +3 −1 index.js
  2. +16 −0 test/resource.test.js
View
4 index.js
@@ -61,7 +61,9 @@ function Resource(name, actions, app) {
// default actions
for (var i = 0, key; i < orderedActions.length; ++i) {
key = orderedActions[i];
- if (actions[key]) this.mapDefaultAction(key, actions[key]);
+ var handler = actions[key];
+ if (typeof handler === 'function') handler = handler.bind(actions);
+ if (handler) this.mapDefaultAction(key, handler);
}
// auto-loader
View
16 test/resource.test.js
@@ -1,6 +1,7 @@
var assert = require('assert')
, express = require('express')
+ , should = require('should')
, Resource = require('..')
, request = require('supertest')
, batch = require('./support/batch');
@@ -55,6 +56,21 @@ describe('app.resource()', function(){
.expect('destroy forum 5', next());
})
+ it('should bind this correctly', function(done) {
+ var app = express();
+ var testController = {
+ 'users': 'userA userB',
+ 'index': function(req, res) {
+ res.send(this.users);
+ }
+ };
+
+ app.resource('users', testController);
+ request(app)
+ .get('/users')
+ .expect('userA userB', done);
+ });
+
it('should support root resources', function(done){
var app = express();
var next = batch(done);
Something went wrong with that request. Please try again.