preloading_data.rst 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. =====================================
  2. Preloading Data for Ember.js Frontend
  3. =====================================
  4. When user visits Misago site for first time, his/hers HTTP request is handled by Django which outputs simple version of requested page. If user has JavaScript enabled in browser, full version of page is then ran by Ember.js.
  5. To keep this process as fast as possible, Misago already includes ("preloads") some of data within initial response. This data is assigned to global ``MisagoData`` object and accessed via ``MisagoPreloadStore`` helper.
  6. Preloading Custom Data
  7. ----------------------
  8. Misago creates empty dict and makes it available as ``preloaded_ember_data`` attribute on ``HttpRequest`` object. This dict is converted into JSON when initial page is rendered by Django.
  9. This means that as long as initial page wasn't rendered yet, you can preload data in any place that has access to request object.
  10. Accessing Preloaded Data
  11. ------------------------
  12. Misago provides utility object defined within ``misago/utils/preloadstore`` module that provides simple API for accessing keys defined in ``MisagoData``::
  13. // some .js module that wants to access preloaded data
  14. import MisagoPreloadStore from 'misago/utils/preloadstore';
  15. // see if required key is defined
  16. if (MisagoPreloadStore.has('thread')) {
  17. // get key value from store
  18. var preloadedThread = MisagoPreloadStore.get('thread');
  19. }
  20. // or use default if key isn't set
  21. var somethingElse = MisagoPreloadStore.get('nonexistantKey', {'default': 'Value'})
  22. // pop value so future code doesn't access it
  23. var preloadedThread = MisagoPreloadStore.pop('thread');
  24. console.log(MisagoPreloadStore.get('thread')); // prints "undefined" in console
  25. // finally set values in store (useful for testing, but terrible for global state)
  26. MisagoPreloadStore.get('fakeValue', {'mock': 'Value'})