Best way to render a view from a script-fired http request?

25 views
Skip to first unread message

Ryan Emberling

unread,
Aug 8, 2016, 3:00:03 PM8/8/16
to sails.js
Hi guys, I know this is a noob question, but I'm wondering what the best way to render a view from a request made not in the url bar of the browser, but from a js method.

I've got a button in one page that uses angular to make the following request when clicked:
$http({
method: 'GET',
url: 'game/gameView',
data: {
gameId: gameId
}
})
.then(
function success (res) {
console.log("Went to gameView");
console.log(res);
},
function error (res) {
        console.log("Bad request")
}); //End of $http()

On the server I'm handling the request with a res.view()

My problem is that the while the the server is responding to the request with the html, the front end doesn't actually change, because I haven't told it WHAT to do with the html it just received from the server. 

Is there a best practice for handling transitions like this? It feels wrong to just replace the whole page manually with something like:
$(html).html(res.data) //using jquery

But is there any other way? Should I use a different layout than the default, so I can receive less than a full page of html (no head, scripts, etc), and just replace the body in the above way? There is no way to have a request like the one I am making above with angular *directly* navigate me to the page served by res.view(), correct? What do you guys recommend?

Thanks for the help!

Core E

unread,
Aug 8, 2016, 3:06:58 PM8/8/16
to Ryan Emberling, sails.js

You can tell res.view to not use a layout then have your front-end replace the html as needed.

Ryan Emberling

unread,
Aug 8, 2016, 3:11:31 PM8/8/16
to sails.js, itsalaid...@gmail.com
Thanks for the quick answer! How do I configure that? Since res.view() only takes 2 parameters (pathToView, locals), I assume this is a global configuration that would disable the use of layouts throughout my application, correct?

Ryan Emberling

unread,
Aug 8, 2016, 3:15:09 PM8/8/16
to sails.js, itsalaid...@gmail.com
Ah, found it. It's in config/views.js 

You can set 'layout' to false, and then all layouts are disabled. This does mean that you can't use them elsewhere, so for anyone else stumbling into this: be aware that changing the page with a button necessitates refraining from using layouts!

Ambroise Dhenain

unread,
Aug 8, 2016, 3:59:49 PM8/8/16
to Ryan Emberling, sails.js
I think you can override it somehow using a options object when loading a view, so that this specific view doesn't use a layout.

I'm pretty sure it's possible, look at how the 404/403 are done on the frontend, should give you something to start with.
--
Cordialement,

M. Ambroise Dhenain.
Reply all
Reply to author
Forward
0 new messages