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

Load component #25

Open
ryanto opened this issue Feb 13, 2018 · 2 comments
Open

Load component #25

ryanto opened this issue Feb 13, 2018 · 2 comments

Comments

@ryanto
Copy link
Member

ryanto commented Feb 13, 2018

{{#load-relationship post 'comments' as |loader|}}

  {{#if loader.isLoading}}
    fetching
  {{/if}}

  {{#if loader.isLoaded}}
    there are {{post.comments.length}} comments
  {{/if}}

{{/load-relationship}}
@Subtletree
Copy link

Subtletree commented Mar 12, 2018

I'm using a basic version of this, no e-concurrency or anything:

export default Component.extend({
  isLoaded: false,
  isLoading: false,
  isError: false,

  didInsertElement() {
    this._super(...arguments);

    if (this.model.hasLoaded(this.relationship)) {
      this.setLoaded();
    } else {
      this.setLoading();
      this.model.load(this.relationship)
        .then(() => this.setLoaded())
        .catch((error) => this.setError(error));
    }
  },

  setLoading() {
    this.setProperties({
      isLoaded: false,
      isLoading: true,
      isError: false
    });
  },

  setLoaded() {
    this.setProperties({
      isLoaded: true,
      isLoading: false,
      isError: false
    });
  },

  setError(error) {
    this.setProperties({
      isLoaded: false,
      isLoading: false,
      isError: error
    });
  }
}).reopenClass({
  positionalParams: ['model', 'relationship']
});

Let me know if you want a PR

@ryanto
Copy link
Member Author

ryanto commented Sep 13, 2018

This is now the highest priority feature in my mind! 👍 🔥

This is important because having a component to load data lets Ember developers keep their templates declarative while also allowing them to explicitly express when data is loaded and how it affects the UI.

If you want, we can start with what you have. I'd love to get this into storefront and iterate on it!

Off the top of my head we'll need to answer a few questions...

  1. What is the API of this template? I've been thinking something like...
{{#load-relationship post 'comments' as |loading error data|}}
 ...
{{/load-relationship}}
  1. How do we handle component tear downs given the asynchronous nature of this component? We need to guard against the component being destroyed before the ajax response comes back from the server.

There's some unknown unknowns here. As we start writing test cases for this component I'm sure we'll uncover new things!

Let me know your thoughts.

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

No branches or pull requests

2 participants