sails.js access controller method from controller

2019-01-14 03:15发布

How come in sails you cannot access other controller methods from within another one?

like this.

module.exports = 

   findStore: ->
       # do somthing

   index: ->
      @findStore(); # Error: undefined

Compiled

module.exports = {
  findStore: function() {},
  index: function() {
    return this.findStore(); // Error: undefined
  }
};

If you can't do this, then why not? how else should I be doing this...

7条回答
太酷不给撩
2楼-- · 2019-01-14 04:06

I would like to suggest a solution that works but not the best possible way to do it. We can use bind function to bind the context with the calling source as shown below :

generateUrl is present in the Controller A

function generateUrl(){
  return 'www.google.com';
}

get URL is another method in Controller A

getURL(){
  A.generateURL.bind(A.generateURL) //func call with optional arg
}

I hope this helps!

查看更多
登录 后发表回答