概述

v0.9.5+ JSService 和 Weex 实例在 JS Runtime 中并行运行。Weex 实例的生命周期可调用 JSService 生命周期。目前提供创建、刷新、销毁生命周期。
JSService 和前端世界里的 vendor.js 一样,它通常用于将每个页面中的重复js函数移至全局环境。 重要提醒: JSService 使用不当会导致内存增高或全局污染!

注册

iOS

  1. [WXSDKEngine registerService:@"SERVICE_NAME" withScript: @"SERVICE_JS_CODE" withOptions: @{}];
    2. // or
    3. [WXSDKEngine registerService:@"SERVICE_NAME" serviceScriptUrl: @"SERVICE_JS_URL" withOptions: @{}];

Android

  1. HashMap<String, String> options = new HashMap<>()
    2. options.put("k1", "v1")
    3. String SERVICE_NAME = "SERVICE_NAME"
    4. String SERVICE_JS_CODE = "SERVICE_JS_CODE"
    5. boolean result = WXSDKEngine.registerService(SERVICE_NAME, SERVICE_JS_CODE, options)

params of options Could have { create, refresh, destroy } lifecycle methods. In create method it should return an object of what variables or classes would be injected into the Weex instance.

Web

  1. <script src="SERVICE_JS_CODE_URL"></script>

样例

  1. service.register(SERVICE_NAME /* same string with native */, {
    2.   /**
    3.     * JSService lifecycle. JSService `create` will before then each instance lifecycle `create`. The return param `instance` is Weex protected param. This object will return to instance global. Other params will in the `services` at instance.
    4.     *
    5.     * @param  {String} id  instance id
    6.     * @param  {Object} env device environment
    7.     * @return {Object}
    8.     */
    9.   create: function(id, env, config) {
    10.     return {
    11.       instance: {
    12.         InstanceService: function(weex) {
    13.           var modal = weex.requireModule('modal')
    14.           return {
    15.             toast: function(title) {
    16.               modal.toast({ message: title })
    17.             }
    18.           }
    19.         }
    20.       },
    21.       NormalService: function(weex) {
    22.         var modal = weex.requireModule('modal')
    23.         return {
    24.           toast: function(title) {
    25.             modal.toast({ message: title })
    26.           }
    27.         }
    28.       }
    29.     }
    30.   },
    31.   32.   /**
    33.     * JSService lifecycle. JSService `refresh` will before then each instance lifecycle `refresh`. If you want to reset variable or something on instance refresh.
    34.     *
    35.     * @param  {String} id  instance id
    36.     * @param  {Object} env device environment
    37.     */
    38.   refresh: function(id, env, config){
    39.   40.   },
    41.   42.   /**
    43.     * JSService lifecycle. JSService `destroy` will before then each instance lifecycle `destroy`. You can deleted variable here. If you doesn't detete variable define in JSService. The variable will always in the js runtime. It's would be memory leak risk.
    44.     *
    45.     * @param  {String} id  instance id
    46.     * @param  {Object} env device environment
    47.     * @return {Object}
    48.     */
    49.   destroy: function(id, env) {
    50.   51.   }
    52. })

Use JSService 1.