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

async view helpers? #693

Open
der-On opened this issue Jun 29, 2015 · 1 comment
Open

async view helpers? #693

der-On opened this issue Jun 29, 2015 · 1 comment
Assignees
Labels

Comments

@der-On
Copy link
Contributor

der-On commented Jun 29, 2015

I know that the partial() view helpers works async, by inserting a placeholder.

Is it possible to create own async view helpers working with the same method?

If not what would be the best place to include such kind of feature?

Possible API:

Using functions: if helper methods returns a function, execute this function right after rendering and pass it a callback function as single argument. the callback should get an error as first and the result of the async helper as second argument:

    function asyncHelper() {
      return function(whenDone) {
        // do your async stuff
        whenDone(null, 'this will get insert'); // <- pass an Error instead of null if an error occured
      }; 
    }

Using promises: if the helper method returns a promise, use its resolve() to replace the placeholder content and the catch() to catch helper errors:

    function promiseHelper() {
      return new Promise(function (resolve, reject) {
        // do async stuff
        resolve('this will get insert'); // <- use catch(Error) to pass an error
      });
    }

Using a wrapping build-in async helper: Geddy will include a build-in async view helper. This helper will do almost the same as the existing partial helper and needs either a promise or function as in the upper cases passed as argument. Template code could then look like this:

<%- async(asyncHelper()) %>

or with promise

<%- async(promiseHelper()) %>

The benefits of this are that we could be able to pass options to the helper that would for example trigger caching of the async result.

@der-On
Copy link
Contributor Author

der-On commented Jun 29, 2015

Work has started in the async-view-helpers branch.

I just tested this in a geddy app and it seems to work. However I currently do not know how to write tests for it. I tried going from the partial tests, but I yet don't know how to actually feed the partials with real template strings: https://github.com/geddy/geddy/blob/async-view-helpers/test/templates/async_partial.js#L10

To explain my approach:

I've used the build-in async() approach and simply created a new helper within the existing Partial code: https://github.com/geddy/geddy/blob/async-view-helpers/lib/template/partial.js#L34

@der-On der-On assigned der-On and unassigned mde Jul 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants