Bladeren bron

Use a promise for getDownloadUrl for cases in which the proper url is
not known client-side.

ian-r-rose 8 jaren geleden
bovenliggende
commit
449fa0b1a6
3 gewijzigde bestanden met toevoegingen van 13 en 8 verwijderingen
  1. 1 1
      src/docregistry/context.ts
  2. 6 5
      src/filebrowser/model.ts
  3. 6 2
      test/src/docregistry/context.spec.ts

+ 1 - 1
src/docregistry/context.ts

@@ -373,7 +373,7 @@ class Context<T extends DocumentRegistry.IModel> implements DocumentRegistry.ICo
   /**
    * Resolve a relative url to a correct server path.
    */
-  resolveUrl(url: string): string {
+  resolveUrl(url: string): Promise<string> {
     // Ignore urls that have a protocol.
     if (utils.urlParse(url).protocol || url.indexOf('//') === 0) {
       return url;

+ 6 - 5
src/filebrowser/model.ts

@@ -235,11 +235,12 @@ class FileBrowserModel implements IDisposable, IPathTracker {
    * @param - path - The path of the file to be downloaded.
    */
   download(path: string): void {
-    let url = this._manager.contents.getDownloadUrl(path);
-    let element = document.createElement('a');
-    element.setAttribute('href', url);
-    element.setAttribute('download', '');
-    element.click();
+    this._manager.contents.getDownloadUrl(path).then((url)=>{;
+      let element = document.createElement('a');
+      element.setAttribute('href', url);
+      element.setAttribute('download', '');
+      element.click();
+    });
   }
 
   /**

+ 6 - 2
test/src/docregistry/context.spec.ts

@@ -388,8 +388,12 @@ describe('docregistry/context', () => {
     describe('#resolveUrl()', () => {
 
       it('should resolve a relative url to a correct server path', () => {
-        let path = context.resolveUrl('./foo');
-        expect(path).to.be(manager.contents.getDownloadUrl('foo'));
+        let resolveUrlPromise = context.resolveUrl('./foo');
+        let getDownloadUrlPromise = manager.contents.getDownloadUrl('foo');
+        Promise.all([resolveUrlPromise, getDownloadUrlPromise])
+        .then((values)=>{
+          expect(values[0]).to.be(values[1]);
+        });
       });
 
       it('should ignore urls that have a protocol', () => {