7. 服务 -- Highway MVVM

Highway框架核心服务解析
本文介绍Highway前端框架中的核心服务,包括内置服务如作用域管理($scope)、定时器($timeout)、HTTP请求($http)及事件($event),同时讲解如何自定义服务,并提供示例代码。

7-1. 内建

7-1-1. $scope

参考2-4. 作用域章节

7-1-2. $timeout

<!-- examples/services/timeout.html -->

<div>something happend after {{timeout}}s</div>

const app = new Highway({
  $el: $('#app'),
  $scope: {
    timeout: 3
  },
  $mount() {
    this.$timeout(() => {
      alert('highway');
    }, 3000);
  }
});

7-1-2. $interval

<!-- examples/services/timeout.html -->

<div>something happend after {{timeout}}s</div>

const app = new Highway({
  $el: $('#app'),
  $scope: {
    timeout: 3
  },
  $mount() {
    this.$timeout(() => {
      alert('highway');
    }, 3000);
  }
});

7-1-3. $http

复用jQuery/Zepto中的ajax方法

  • http.ajax
  • get.get
  • post.post
  • json.getJSON
  • jsonp.ajaxJSONP
  • settings.ajaxSettings
  • settings:jQuery.ajaxSetup/.ajaxSettings
<!-- examples/services/http.html -->

<ul>
  <li hi-repeat="user in users">id: {{user.id}}, name: {{user.name}}</li>
</ul>

const app = new Highway({
  $el: $('#app'),
  $scope: {},
  $mount() {
    this.$http({
      type: 'get',
      url: './users.json',
      success: (data, status, xhr) => {
        this.$set('users', data.users)
      },
      error: (xhr, errorType, error) => {
        alert('error');
      }
    })
  }
});

7.1.4. $event

参阅6-3. 通信

7-2. 自定义

自定义服务是一个工厂函数

7-2-1. 入参

  • $ctx: 当前上下文

7-2-2. 出参

可选。如返回,可通过this.$servcies[name]引用

  • $mount: 生命周期函数,挂载时调用

  • $unmount: 生命周期函数,销毁时调用

7-2-3. 全局

使用Highway.service定义,需指定宏服务名和服务工厂函数

7-2-4. 局部

指定视图中有效,通过$services指定,需指定宏指令名和宏指令工厂函数

7-2-5. 示例

定义一个cookie读写服务

<!-- examples/services/customized.html -->

<div id="app">
  <button hi-on:click="clickMe">destroy me</button>
</div>

const cookie = function ({$ctx}) {
  return $ctx.$cookie = {
    $mount() {
      console.log('>>> cookie services mounted')
    },
    $get(key) {
      const reg = new RegExp(`(^| )${key}=([^;]*)(;|$)`);
      const matches = document.cookie.match(reg);
      if(matches) {
        return matches[2]
      }

    },
    $set(key, value) {
      document.cookie = `${key}=${value}`;
    },
    $unmount() {
      console.log('>>> cookie services unmounted')
    },
    destroy() {
      this.$destory();
    }
  }
};

// 全局管道
//Highway.service('$cookie', cookie);

const app = new Highway({

  $el: $('#app'),
  $scope: {
    ratio: 0.95
  },
  // 局部管道
  $services: {
    $cookie: cookie
  },
  $mount() {
    this.$cookie.$set('cookie_0', 'highway');
    this.$cookie.$set('cookie_1', 'hello world');

    // 可通过$service引用
    console.log('cookie_1 value is:' + this.$services['$cookie'].$get('cookie_1'));
  },
  clickMe() {
    this.$destroy();
  }
});
(.venv) PS C:\Users\chen4\PycharmProjects\pythonProject1\.venv\Scripts> pip install gluonnlp Collecting gluonnlp Using cached gluonnlp-0.10.0.tar.gz (344 kB) Preparing metadata (setup.py) ... done Requirement already satisfied: numpy>=1.16.0 in c:\users\chen4\pycharmprojects\pythonproject1\.venv\lib\site-packages (from gluonnlp) (1.26.4) Collecting cython (from gluonnlp) Using cached cython-3.1.2-cp312-cp312-win_amd64.whl.metadata (6.0 kB) Requirement already satisfied: packaging in c:\users\chen4\pycharmprojects\pythonproject1\.venv\lib\site-packages (from gluonnlp) (24.1) Using cached cython-3.1.2-cp312-cp312-win_amd64.whl (2.7 MB) Building wheels for collected packages: gluonnlp DEPRECATION: Building 'gluonnlp' using the legacy setup.py bdist_wheel mechanism, which will be removed in a future version. pip 25.3 will enforce this behaviour change. A possib le replacement is to use the standardized build interface by setting the `--use-pep517` option, (possibly combined with `--no-build-isolation`), or adding a `pyproject.toml` file to the source tree of 'gluonnlp'. Discussion can be found at https://github.com/pypa/pip/issues/6334 Building wheel for gluonnlp (setup.py) ... error error: subprocess-exited-with-error × python setup.py bdist_wheel did not run successfully. │ exit code: 1 ╰─> [136 lines of output] C:\Users\chen4\PycharmProjects\pythonProject1\.venv\Lib\site-packages\setuptools\__init__.py:92: _DeprecatedInstaller: setuptools.installer and fetch_build_eggs are deprecated. !! ******************************************************************************** Requirements should be satisfied by a PEP 517 installer. If you are using pip, you can try `pip install --use-pep517`. By 2025-Oct-31, you need to update your project and remove deprecated calls or your builds will no longer be supported. ******************************************************************************** !! dist.fetch_build_eggs(dist.setup_requires) running bdist_wheel running build running build_py creating build\lib.win-amd64-cpython-312\gluonnlp copying src\gluonnlp\base.py -> build\lib.win-amd64-cpython-312\gluonnlp copying src\gluonnlp\_constants.py -> build\lib.win-amd64-cpython-312\gluonnlp copying src\gluonnlp\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp creating build\lib.win-amd64-cpython-312\gluonnlp\calibration copying src\gluonnlp\calibration\collector.py -> build\lib.win-amd64-cpython-312\gluonnlp\calibration copying src\gluonnlp\calibration\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\calibration creating build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\baidu_ernie_data.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\candidate_sampler.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\classification.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\conll.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\dataloader.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\dataset.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\datasetloader.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\glue.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\intent_slot.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\question_answering.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\registry.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\sampler.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\sentiment.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\stream.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\super_glue.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\transforms.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\translation.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\utils.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\word_embedding_evaluation.py -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\data creating build\lib.win-amd64-cpython-312\gluonnlp\embedding copying src\gluonnlp\embedding\evaluation.py -> build\lib.win-amd64-cpython-312\gluonnlp\embedding copying src\gluonnlp\embedding\token_embedding.py -> build\lib.win-amd64-cpython-312\gluonnlp\embedding copying src\gluonnlp\embedding\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\embedding creating build\lib.win-amd64-cpython-312\gluonnlp\initializer copying src\gluonnlp\initializer\initializer.py -> build\lib.win-amd64-cpython-312\gluonnlp\initializer copying src\gluonnlp\initializer\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\initializer creating build\lib.win-amd64-cpython-312\gluonnlp\loss copying src\gluonnlp\loss\activation_regularizer.py -> build\lib.win-amd64-cpython-312\gluonnlp\loss copying src\gluonnlp\loss\label_smoothing.py -> build\lib.win-amd64-cpython-312\gluonnlp\loss copying src\gluonnlp\loss\loss.py -> build\lib.win-amd64-cpython-312\gluonnlp\loss copying src\gluonnlp\loss\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\loss creating build\lib.win-amd64-cpython-312\gluonnlp\metric copying src\gluonnlp\metric\length_normalized_loss.py -> build\lib.win-amd64-cpython-312\gluonnlp\metric copying src\gluonnlp\metric\masked_accuracy.py -> build\lib.win-amd64-cpython-312\gluonnlp\metric copying src\gluonnlp\metric\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\metric creating build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\attention_cell.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\bert.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\bilm_encoder.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\block.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\convolutional_encoder.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\elmo.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\highway.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\info.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\language_model.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\lstmpcellwithclip.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\parameter.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\sampled_block.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\seq2seq_encoder_decoder.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\sequence_sampler.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\transformer.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\translation.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\utils.py -> build\lib.win-amd64-cpython-312\gluonnlp\model copying src\gluonnlp\model\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\model creating build\lib.win-amd64-cpython-312\gluonnlp\optimizer copying src\gluonnlp\optimizer\bert_adam.py -> build\lib.win-amd64-cpython-312\gluonnlp\optimizer copying src\gluonnlp\optimizer\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\optimizer creating build\lib.win-amd64-cpython-312\gluonnlp\utils copying src\gluonnlp\utils\files.py -> build\lib.win-amd64-cpython-312\gluonnlp\utils copying src\gluonnlp\utils\parallel.py -> build\lib.win-amd64-cpython-312\gluonnlp\utils copying src\gluonnlp\utils\parameter.py -> build\lib.win-amd64-cpython-312\gluonnlp\utils copying src\gluonnlp\utils\seed.py -> build\lib.win-amd64-cpython-312\gluonnlp\utils copying src\gluonnlp\utils\version.py -> build\lib.win-amd64-cpython-312\gluonnlp\utils copying src\gluonnlp\utils\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\utils creating build\lib.win-amd64-cpython-312\gluonnlp\vocab copying src\gluonnlp\vocab\bert.py -> build\lib.win-amd64-cpython-312\gluonnlp\vocab copying src\gluonnlp\vocab\elmo.py -> build\lib.win-amd64-cpython-312\gluonnlp\vocab copying src\gluonnlp\vocab\subwords.py -> build\lib.win-amd64-cpython-312\gluonnlp\vocab copying src\gluonnlp\vocab\vocab.py -> build\lib.win-amd64-cpython-312\gluonnlp\vocab copying src\gluonnlp\vocab\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\vocab creating build\lib.win-amd64-cpython-312\gluonnlp\data\batchify copying src\gluonnlp\data\batchify\batchify.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\batchify copying src\gluonnlp\data\batchify\embedding.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\batchify copying src\gluonnlp\data\batchify\language_model.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\batchify copying src\gluonnlp\data\batchify\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\batchify creating build\lib.win-amd64-cpython-312\gluonnlp\data\bert copying src\gluonnlp\data\bert\glue.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\bert copying src\gluonnlp\data\bert\squad.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\bert copying src\gluonnlp\data\bert\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\bert creating build\lib.win-amd64-cpython-312\gluonnlp\data\corpora copying src\gluonnlp\data\corpora\google_billion_word.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\corpora copying src\gluonnlp\data\corpora\large_text_compression_benchmark.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\corpora copying src\gluonnlp\data\corpora\wikitext.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\corpora copying src\gluonnlp\data\corpora\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\corpora creating build\lib.win-amd64-cpython-312\gluonnlp\data\xlnet copying src\gluonnlp\data\xlnet\squad.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\xlnet copying src\gluonnlp\data\xlnet\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\data\xlnet creating build\lib.win-amd64-cpython-312\gluonnlp\model\train copying src\gluonnlp\model\train\cache.py -> build\lib.win-amd64-cpython-312\gluonnlp\model\train copying src\gluonnlp\model\train\embedding.py -> build\lib.win-amd64-cpython-312\gluonnlp\model\train copying src\gluonnlp\model\train\language_model.py -> build\lib.win-amd64-cpython-312\gluonnlp\model\train copying src\gluonnlp\model\train\__init__.py -> build\lib.win-amd64-cpython-312\gluonnlp\model\train running egg_info writing src\gluonnlp.egg-info\PKG-INFO writing dependency_links to src\gluonnlp.egg-info\dependency_links.txt writing requirements to src\gluonnlp.egg-info\requires.txt writing top-level names to src\gluonnlp.egg-info\top_level.txt reading manifest file 'src\gluonnlp.egg-info\SOURCES.txt' reading manifest template 'MANIFEST.in' warning: no files found matching '*.py' under directory 'gluonnlp' warning: no previously-included files matching '*' found under directory 'tests' warning: no previously-included files matching '*' found under directory 'scripts' adding license file 'LICENSE' writing manifest file 'src\gluonnlp.egg-info\SOURCES.txt' copying src\gluonnlp\data\fast_bert_tokenizer.c -> build\lib.win-amd64-cpython-312\gluonnlp\data copying src\gluonnlp\data\fast_bert_tokenizer.pyx -> build\lib.win-amd64-cpython-312\gluonnlp\data running build_ext Compiling src/gluonnlp/data/fast_bert_tokenizer.pyx because it changed. [1/1] Cythonizing src/gluonnlp/data/fast_bert_tokenizer.pyx building 'gluonnlp.data.fast_bert_tokenizer' extension error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/ [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for gluonnlp Running setup.py clean for gluonnlp Failed to build gluonnlp ERROR: Failed to build installable wheels for some pyproject.toml based projects (gluonnlp) 以上为安装gluonnlp的报错结果
06-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值