0

Are there a good way to get async data from an EJS file?
Sails.js only have async methods to get data from a database.

I have a collection of Pages with their associated content Values. In some cases I want to get a specific Value from another Page (e.g.: in a related page module, a navigation module...). And I need to do that server side, to keep the front-end part SEO friendly.

The easier way should be to retrieve all the Pages and Values inside the controller, and then expose a function to my EJS to get the specific data I need. But I really don't want to retrieve each time all the data from a collection.

I'm working on a CMS so I would avoid creating a new controller/view for each specific cases that I will have.

EDIT:
It seems there is no good way to do that, so I reworked my controller to make it a bit more intelligent. It solve my problem for now.

Etienne
  • 2,257
  • 3
  • 27
  • 41
  • What kind of data are you trying to retrieve in your view? Sounds like something you would use AJAX or websockets for. – sgress454 Apr 02 '14 at 22:21
  • I have a collection of Pages with their associated content Values. In some cases I want to get a specific Value from another Page (e.g.: in a related page module, a navigation module...). I need to do that server side, to keep the front-end part SEO friendly. – Etienne Apr 02 '14 at 22:42
  • @ScottGress I have updated my question with more details. – Etienne Apr 03 '14 at 17:09

1 Answers1

1

Assuming I understand you correctly....

data is sent to the view by passing an object into the view funciton

return res.view({data: "important data"});

I'm guessing that you want to call a method for some specific data mid-render. Because having all the data ready on the res.view() call is too slow. As far as I know this is not possible. That would almost certainly be "blocking" code.

Best option I could recommend is to grab specific data using ajax on the client after page load. One of the best features of using sails :)

InternalFX
  • 1,475
  • 12
  • 14
  • Thanks for your answer. I still need to do that server side, to keep it SEO friendly. I have updated my question with more details. – Etienne Apr 03 '14 at 17:15
  • 1
    @Etienne it's still a little vague...but it sounds like you need some clever way to know ahead of time what data you need, before you start the render phase. Once you start rendering your view. Your controller is done. – InternalFX Apr 03 '14 at 17:37
  • Thanks @internalFX. I reworked my controller to make it more intelligent, it solve my problem for now. – Etienne Apr 17 '14 at 20:42