开发者

How can I assemble different parts of a view with Express before sending it back?

开发者 https://www.devze.com 2023-04-05 04:01 出处:网络
I have a project and I need to send back rendered HTML via an AJAX call. I know I can use re开发者_如何学Cs.render(\'myview\', {...}); to pass the data back as fully rendered HTML. But I have to rende

I have a project and I need to send back rendered HTML via an AJAX call. I know I can use re开发者_如何学Cs.render('myview', {...}); to pass the data back as fully rendered HTML. But I have to render 3 different parts of the page and I want to encapsulate all of those three parts into one response.

So that leaves me with one of two options possibly. Either I can render the view and NOT send it then use res.write to send all the views once they're ready or I can do 3 different AJAX calls. I'd prefer the first one. Any ideas on how to get started?


  res.header 'Content-Type','application/json'
  res.partial 'partials/categories', {categories: categories}, (err, categoriesHtml) ->
    res.partial 'partials/items', {items: items}, (err, itemsHtml) ->
      htmlResponse = 
        categories: categoriesHtml
        items: itemsHtml
      htmlResponse = JSON.stringify htmlResponse
      res.send htmlResponse

Partial is right!

0

精彩评论

暂无评论...
验证码 换一张
取 消