fuse-box / fuse-box-aurelia-seed

MIT License
11 stars 1 forks source link

users.ts #11

Closed vegarringdal closed 7 years ago

vegarringdal commented 7 years ago

@arjendeblok

Why not keep it as possible to original ? Any special reasons?

fusebox-seed:

import {lazy} from 'aurelia-framework';
import {HttpClient} from 'aurelia-fetch-client';

// polyfill fetch client conditionally
const fetch = !self.fetch ? System.import('isomorphic-fetch') : Promise.resolve(self.fetch);

interface IUser {
  avatar_url: string;
  login: string;
  html_url: string;
}

export class Users {
  heading: string = 'Github Users';
  users: Array<IUser> = [];
  http: HttpClient;

  constructor(@lazy(HttpClient) private getHttpClient: () => HttpClient) {}

  async activate(): Promise<void> {
    // ensure fetch is polyfilled before we create the http client
    await fetch;
    const http = this.http = this.getHttpClient();

    http.configure(config => {
      config
        .useStandardConfiguration()
        .withBaseUrl('https://api.github.com/');
    });

    const response = await http.fetch('users');
    this.users = await response.json();
  }
}

Original:

import {autoinject} from 'aurelia-framework';
import {HttpClient} from 'aurelia-fetch-client';
import 'fetch';

@autoinject
export class Users {
  public heading = 'Github Users';
  public users = [];

  constructor(private http: HttpClient) {
    http.configure(config => {
      config
        .useStandardConfiguration()
        .withBaseUrl('https://api.github.com/');
    });
  }

  public activate() {
    return this.http.fetch('users')
      .then(response => response.json())
      .then(users => this.users = users as any);
  }
}

Original works fine in ie11 too https://vegarringdal.github.io/aurelia-fusebox-test/

arjendeblok commented 7 years ago

Where did you get that 'original' then.

Mine is from https://github.com/aurelia/skeleton-navigation/blob/master/skeleton-typescript-webpack/src/users.ts

When using typescript the idea is to use the async/await pattern.

arjendeblok commented 7 years ago

There is also a bug in there. System.Import should be require to be able to run in IE11.

vegarringdal commented 7 years ago

At the skeleton-typescript 😄 https://github.com/aurelia/skeleton-navigation/blob/master/skeleton-typescript/src/users.ts

vegarringdal commented 7 years ago

Actually when testing newest fuse on my test repo it somehow breaks and loader fails somehow. Are you having issues like this on v 121 ? but 117 works

@nchanged Have you done anything on fuse how it bundles some parts lately ?

vegarringdal commented 7 years ago

From what I can tell it happens in v118, user.html is missing locals function

vegarringdal commented 7 years ago

@nchanged never mind, must be something Im doing, this seed works fine

Ill do more testing and try and figure out why my is breaking after v118, I have the routes in own folder, maybe that is what breaks it, also bundle is in folder

vegarringdal commented 7 years ago

@arjendeblok

Found my bug in the end, for some reason having variable "http" in constructor failed. Nice to know about incase you copy code from one of the other skeletons :-)

Now Ive updated this to use async await too: https://github.com/vegarringdal/aurelia-fusebox-test/blob/master/src/routes/users.ts#L19-L21

https://vegarringdal.github.io/aurelia-fusebox-test/

arjendeblok commented 7 years ago

I think that the issue was that you designated the method as async and did not use await. If you did not set async in the method then you can use the old promises way with .then()

vegarringdal commented 7 years ago

No the "http" issue was a fusebox bug, got fixed in 122.preview4, there had been a similar with "https" so @nchange knew what to do.

nchanged commented 7 years ago

yeah it's all fixed in 122-preview.4