2. 视图 -- Highway MVVM

Highway框架详解
本文介绍了一个名为Highway的前端框架,详细阐述了如何创建视图实例及其提供的多种属性和方法,包括生命周期函数、数据绑定方式及作用域管理等。通过具体示例展示了双向绑定、单向绑定的区别以及如何进行属性的获取、设置和监听。

通过new Highway可创建视图实例

const app = new Highway({
    $el: $('#app'),
    $scope: {
      name: 'hello world'
    }
  });

2.1 属性/方法

  • $el: 元素。
  • $template:模板字符串。
  • $scope:初始化数据。
  • $willmount:生命周期函数,编译前调用。不常用
  • $mount:生命周期函数,编译完成条用。常用
  • didmountmount之后。不常用
  • $destory:销毁函数,调用销毁当前实例。
  • $willunmount:生命周期函数,销毁前调用。不常用
  • $unmount:生命周期函数,销毁完成后调用。常用
  • didunmountunmount之后。不常用
<!-- examples/views/prop-function.html -->

<div id="app">
</div>

<script id="template" type="text/template">
  <div>i will destory after {{timeout}} seconds</div>
</script>

const app = new Highway({
  $el: $('#app'),
  $template: $('#template').html(),
  $scope: {
    timeout: 3
  },
  $willmount() {
    console.log('>>>::$willmount');
  },
  $mount() {
    console.log('>>>::$mount');

    this.$interval(() => {
     const timeout = this.$scope.$get('timeout') - 1;
     if (timeout >= 0) {
        this.$scope.$set('timeout', timeout);
     } else {
        this.$destroy();
     }
    }, 1000);
  },
  $didmount() {
    console.log('>>>::$didmount');
  },
  $willunmount() {
    console.log('>>>::$willunmount');
  },
  $unmount() {
    console.log('>>>::$unmount');
  },
  $didunmount() {
    console.log('>>>::$didunmount');
  },
});

2.2 绑定

2-2-1. 双向绑定

2-2-1-1. 安全

采用{{ property }}或者不指定{{ }}

<!-- examples/views/data-bind.html -->

// html
<p>{{msg}}</p> // '&lt;script&gt;alert(1)'

// js
const app = new Highway({
  $el: $('#app'),
  $scope: {
    msg: '<script>alert(1)'
  }
});
2-2-1-2. 非安全

采用{{{ property }}}

<!-- examples/views/data-bind.html -->

<p>{{{msg}}}</p> // '<script>alert(1)'

2-2-2. 单向绑定

模板引擎,编译时根据scopescope更新,以便节约内存,提高性能

2-2-2-1. 安全

采用[[ property ]]

<!-- examples/views/data-bind.html -->

<p>[[msg]]</p> // '&lt;script&gt;alert(1)'
2-2-2-1. 非安全

采用[[[ property ]]]

<!-- examples/views/data-bind.html -->

<p>[[[msg]]]</p> // '<script>alert(1)'

2.4 作用域

使用$scope作为MVVM框架中的Model

<!-- examples/views/scope.html -->

<div id="app">
  <input type="text" hi-value="name"/>
  <p>{{name}}</p>
</div>  

const app = new Highway({
  $el: $('#app'),
  $scope: {
    name: 'highway'
  }
});

2-4-1. $get

使用scope.get(prop)方法获取属性值,缩写为$get

<!-- examples/views/get.html -->

<div id="app">
  <input type="text" hi-value="user.name"/>
  <button hi-on:click="showMe">showMe</button>
</div>  

const app = new Highway({
  $el: $('#app'),
  $scope: {
    user: {
      name: 'highway'
    }
  },
  showMe() {
    // equal to this.$scope.$get
    alert(this.$get('user.name'));
  }
});

如是嵌套属性,可通过属性分割符”.”获取,例如this.scope.get(‘user.name’),$set类同

如是数组属性,可通过[int]数组下标获取,例如this.scope.get(‘students[2].name’),$set类同

2-4-2. $set

使用scope.set(prop, value)方法获取属性值,缩写为$get

<!-- examples/views/set.html -->

<div id="app">
  <input type="text" hi-value="user.name"/>
  <button hi-on:click="changeMe">changeMe</button>
</div>  

const app = new Highway({
  $el: $('#app'),
  $scope: {
    user: {
      name: 'highway'
    }
  },
  changeMe() {
    // equal to this.$scope.$set
    this.$set('user.name', 'xiage');
  }
});

2-4-3. $watch

使用scope.watch(prop, handler)监控属性变化,缩写为$watch,返回unwather句柄,调用可解除监听

<!-- examples/views/watch.html -->

<div id="app">
  <input type="text" hi-value="name"/>
  <p>{{name}}</p>
  <p>{{logs}}</p>
</div>  

const app = new Highway({
  $el: $('#app'),
  $scope: {
    name: 'highway'
  },
  $mount() {
    const unwatcher = this.$watch('name', (newVal, oldVal) => {
      this.$set('logs', `value changed, newVal is: ${newVal}, oldVal is: ${oldVal}`);
    });

    // remove wather
    //unwatcher();
  }
});

2-4-4. $unwatch

使用scope.unwatch(prop, [handler])监控属性变化,可缩写为$unwatch,如handler不指定,则移除prop上所有的监听器

<!-- examples/views/unwatch.html -->

<div id="app">
  <input type="text" hi-value="name"/>
  <p>{{name}}</p>
  <p>{{logs}}</p>
  <button hi-on:click="unwatch">unwatch</button>
</div>

const app = new Highway({
  $el: $('#app'),
  $scope: {
    name: 'highway'
  },
  $mount() {
    this.$watch('name', (newVal, oldVal) => {
      this.$set('logs', `value changed, newVal is: ${newVal}, oldVal is: ${oldVal}`);
    });
  },
  unwatch() {
    this.$unwatch('name');
  }
});

2-4-5. 常量

单引号或双引号标记的值为常量,并不会从$scope值取值

<!-- examples/views/const.html -->

<div id="app">
  <input type="text" hi-value="user.name"/>
  <p>{{user.name}}</p>
  <p>{{'user.name'}}</p>
  <p>{{"user.name"}}</p>
</div>

const app = new Highway({
  $el: $('#app'),
  $scope: {
    user: {
      name: 'highway'
    }
  }
});

2-5. 类定义

可通过Highway.extend抽取为自定义类

<!-- examples/views/klass.html -->

<div id="app-0"></div>
<div id="app-1"></div>

<script id="t-app" type="text/template">
  <p>i am {{name}}</p>
</script>

const App = Highway.extend({
  $template: $('#t-app').html(),
  $scope: {
    name: 'highway'
  }
});

const app0 = new App({
  $el: $('#app-0')
});

const app1 = new App({
  $el: $('#app-1')
});
第三方支付功能的技术人员;尤其适合从事电商、在线教育、SaaS类项目开发的工程师。; 使用场景及目标:① 实现微信与支付宝的Native、网页/APP等主流支付方式接入;② 掌握支付过程中关键的安全机制如签名验签、证书管理与敏感信息保护;③ 构建完整的支付闭环,包括下单、支付、异步通知、订单状态更新、退款与对账功能;④ 通过定时任务处理内容支付超时与概要状态不一致问题:本文详细讲解了Java,提升系统健壮性。; 阅读应用接入支付宝和建议:建议结合官方文档与沙微信支付的全流程,涵盖支付产品介绍、开发环境搭建箱环境边学边练,重点关注、安全机制、配置管理、签名核心API调用及验签逻辑、异步通知的幂等处理实际代码实现。重点与异常边界情况;包括商户号与AppID获取、API注意生产环境中的密密钥与证书配置钥安全与接口调用频率控制、使用官方SDK进行支付。下单、异步通知处理、订单查询、退款、账单下载等功能,并深入解析签名与验签、加密解密、内网穿透等关键技术环节,帮助开发者构建安全可靠的支付系统。; 适合人群:具备一定Java开发基础,熟悉Spring框架和HTTP协议,有1-3年工作经验的后端研发人员或希望快速掌握第三方支付集成的开发者。; 使用场景及目标:① 实现微信支付Native模式与支付宝PC网页支付的接入;② 掌握支付过程中核心的安全机制如签名验签、证书管理、敏感数据加密;③ 处理支付结果异步通知、订单状态核对、定时任务补偿、退款及对账等生产级功能; 阅读建议:建议结合文档中的代码示例与官方API文档同步实践,重点关注支付流程的状态一致性控制、幂等性处理和异常边界情况,建议在沙箱环境中完成全流程测试后再上线。
(.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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值