Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR] Add support for the View.prepare() logic #756

Merged
merged 4 commits into from
Nov 2, 2015
Merged

Conversation

fzaninotto
Copy link
Member

view.prepare(Function) adds a function to be executed before the view renders.

This is the ideal place to prefetch related entities and manipulate the DataStore. The function can be asynchronous, in which case it should return a Promise.

The prepare function is invoked using Angular's dependency injection system, with a context offering the following services:

  • query: the query object (an object representation of the main request query string)
  • datastore: where the Entries are stored. The dataStore is accessible during rendering
  • view: the current View object
  • entry: the current Entry instance (except in listView)
  • entries: the current list of Entry instances (only in listView)
  • Entry: the Entry constructor (required to transform an object from the REST response to an Entry)
  • window: the window object. If you need to fetch anything other than an entry and pass it to the view layer, it's the only way.

Of course, regular Angular services (like Restangular) are also available.

post.listView().prepare(['datastore', 'entries', 'Entry', function(datastore, entries, Entry ) {
  const authorIds = entries.map(post => post.values.authorId).join(',');
  return fetch('http://myapi.com/authors?id[]=' + authorIds)
     .then(response => response.json())
     .then(authors => Entry.createArrayFromRest(
         authors,
         [new Field('first_name'), new Field('last_name')],
         'author'
     ))
     .then(authorEntries => datastore.setEntries('authors', authorEntries));
}])
  • Add call to prepare() in routing
  • Document usage
  • Add working example
  • Add tests

@fzaninotto fzaninotto changed the title [WIP] Add support for the View.prepare() logic [RFR] Add support for the View.prepare() logic Nov 1, 2015
@fzaninotto
Copy link
Member Author

switching to RFR

jpetitcolas added a commit that referenced this pull request Nov 2, 2015
[RFR] Add support for the `View.prepare()` logic
@jpetitcolas jpetitcolas merged commit bd28d9f into master Nov 2, 2015
@jpetitcolas jpetitcolas deleted the prepare branch November 2, 2015 08:16
@tal-ni
Copy link

tal-ni commented Jun 1, 2016

from which version is the prepare function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants