You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@weex.apache.org by ha...@apache.org on 2018/05/09 03:20:48 UTC

incubator-weex git commit: + [jsfm] support import script dynamically at runtime

Repository: incubator-weex
Updated Branches:
  refs/heads/master 4b9a9daac -> 6d2bdd0c8


+ [jsfm] support import script dynamically at runtime


Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/6d2bdd0c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/6d2bdd0c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/6d2bdd0c

Branch: refs/heads/master
Commit: 6d2bdd0c8f3c5ca1da926ab94d9348d6ce6a41a2
Parents: 4b9a9da
Author: Hanks <zh...@gmail.com>
Authored: Wed May 9 11:08:34 2018 +0800
Committer: Hanks <zh...@gmail.com>
Committed: Wed May 9 11:08:34 2018 +0800

----------------------------------------------------------------------
 runtime/api/WeexInstance.js | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/6d2bdd0c/runtime/api/WeexInstance.js
----------------------------------------------------------------------
diff --git a/runtime/api/WeexInstance.js b/runtime/api/WeexInstance.js
index 6d1607a..de45c69 100644
--- a/runtime/api/WeexInstance.js
+++ b/runtime/api/WeexInstance.js
@@ -60,6 +60,7 @@ export default class WeexInstance {
     this.config = config || {}
     this.document = new Document(id, this.config.bundleUrl)
     this.requireModule = this.requireModule.bind(this)
+    this.importScript = this.importScript.bind(this)
     this.isRegisteredModule = isRegisteredModule
     this.isRegisteredComponent = isRegisteredComponent
   }
@@ -128,6 +129,31 @@ export default class WeexInstance {
     return null
   }
 
+  importScript (src, options = {}) {
+    const id = getId(this)
+    const taskCenter = getTaskCenter(id)
+    return new Promise((resolve, reject) => {
+      if (!taskCenter || typeof taskCenter.send !== 'function') {
+        reject(new Error(`[JS Framework] Failed to import script "${src}", `
+          + `no taskCenter (${id}) matched.`))
+      }
+      try {
+        taskCenter.send('module', {
+          module: 'script',
+          method: 'importScript'
+        }, [src, options], {
+          callback: [
+            result => resolve(result),
+            error => reject(error)
+          ]
+        })
+      }
+      catch (e) {
+        reject(e)
+      }
+    })
+  }
+
   // registerStyleSheet (styles) {
   //   if (this.document) {
   //     this.document.registerStyleSheet(styles)