Ben Kilah
Ben Kilah

Reputation: 3475

Sails.js template helper (filter) with promise

I'm just getting into Sails.js and was wondering if there was a way with the EJS templating system to have a helper function run which includes a promise or database query. This works:

in config/http.js:

module.exports.http = {
  // ...
  locals: {
    filters: {
      testHelper: function(input) { return input; }
    }
  }
}

This does not:

module.exports.http = {
  // ...
  locals: {
    filters: {
      testHelper: function(input) {
            MyModal.create({test:'test'}).exec(function(err,mm){
                  return input+'-'+mm.test;
              });
          }
    }
  }
}

The first always returns "check", the second always returns "undefined" when used in a template "{{ 'check' | testHelper }}"

Upvotes: 1

Views: 278

Answers (1)

Meeker
Meeker

Reputation: 5979

You could using a library like wait.for to wrap and return the promise. I would suggest creating your "helper" as a service and accessing it off the global scope instead of injecting it in your view.

Upvotes: 1

Related Questions