Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom collection actions #39

Open
icambron opened this issue Nov 30, 2011 · 2 comments
Open

custom collection actions #39

icambron opened this issue Nov 30, 2011 · 2 comments

Comments

@icambron
Copy link

Suppose I've done something like:

var users = {

  show: function(req, res){
    //do something
  },

  me: function(req, res){
    //do something
  },

  load : function(id, fn){
    User.findById(id, fn);
  }
};

var userResource = app.resource('users', users);
userResource.map('get', 'me', users.me) //expecting /users/me to do something

That doesn't work. It seems to think that "me" is the ID and passes it to load, thinking it's going to invoke show. In fact, I don't know how it would know any better. So I'm thinking there's no way to do custom collection actions; you can only do custom member actions.

So first off, is that right? If so, is this something I should patch?

@sbugzu
Copy link

sbugzu commented Dec 8, 2011

I found that it's priority problem of register, I have just a temporary solution. modify the sourcecode

//default actions
for (var i=0, key; i < orderedActions.length; i++) {  
  key = orderedActions[i];
  if (actions[key]) this.mapDefaultAction(key, actions[key]);
}

to

//default actions
_this = this;
setTimeout(function() {
  for (var i=0, key; i < orderedActions.length; i++) {
    key = orderedActions[i];
    if (actions[key]) _this.mapDefaultAction(key, actions[key]);
  }
}, 1000);

The purose of modification is delayed loading default actions, and loading custom router at first. You can change the setTimeout time according to the actual.

@wright-io
Copy link

This really does seem like a valid issue. Is the code above really the recommended way to fix it? The problem is that adding something like

userResource.map('get', '/logout', users.logout)

gets blocked by the :show action (expecting that the '/logout' string is an id).

What is the correct way to avoid this conflict?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants