keithamoss / hansardalerts

@HansardAlerts for Western Australia's Hansard
MIT License
2 stars 0 forks source link

Refactor the initial data load #31

Open keithamoss opened 10 years ago

keithamoss commented 10 years ago

Bad: Loading Your Initial Page View With AJAX Calls

Here's a common scenario with AngularJS: the user visits your page, and is greeted with a weird layout, that eventually resolves itself to what you intended. AngularJS has to do a large number of passes on your scopes to ensure that your models have "settled". So your visitor is confused, because the HTML seems to have rendered, wrongly, but then gets the AJAX data, re-runs your models, and everything updates to look fine.

Better: Ask Your Services To Talk To $window

You're already talking to your server, and know what you should get, because you already had the user interaction (the page visit). What I do is I get my initial load data server side, and then pass into Angular by attaching it to the window object. Then in Angular, I have a service whose job it is to fetch that information from the window and provide it to my app in a dependency injectable way. Because $window is injectable, I can then test by attaching whatever input data I want.

From http://miceplans.net/node/36