NWJS温故而知新

作者: 技术与健康 | 来源:发表于2018-01-21 10:42 被阅读18次

去年基于NW.js 0.12.3做过一个项目,当时对于NW中的contexts的理解比较模糊。今天在twiiter上看到发布新的版本,重新又打开了nwjs.io网站的文档部分,落地页面是NW中的contexts的介绍。这里做一整理,加深理解。

首先理解Contexts的概念,概念是对一个事物最准确的诠释和定义。

Contexts in NW.js

NW.js is based on the architecture of Chrome Apps. Thus an invisible background page is loaded automatically at start. And when a new window is created, a JavaScript context is created as well.

In NW.js, Node.js modules can be loaded in the context running in background page, which is the default behavior. Also they can be loaded within the context of each window or frame when running as Mixed Context Mode. Continue to read following sections to see the differences between Separate Context Mode and Mixed Context Mode.

Separate Context Mode

Besides the contexts created by browsers, NW.js introduced additional Node context for running Node modules in the background page by default. So NW.js has two types of JavaScript contexts: Browser Context and Node Context.

ccess Browser and NW.js API in Node Context

In Node context, there are no browser side or NW.js APIs, such as alert() or document.* or nw.Clipboard etc. To access browser APIs, you have to pass the corresponding objects, such as window object, to functions in Node context.

See following example for how to achieve this.

Following script are running in Node context (myscript.js):

// `el` should be passed from browser context
exports.setText = function(el) {
    el.innerHTML = 'hello';
};

In the browser side (index.html):

<div id="el"></div>
<script>
var myscript = require('./myscript');
// pass the `el` element to the Node function
myscript.setText(document.getElementbyId('el'));
// you will see "hello" in the element
</script>

window in Node Context

There is a window object in Node context pointing to the DOM window object of the background page.

Mixed Context Mode

Mixed context is introduced in NW.js 0.13. When running NW.js with --mixed-context CLI option, a new Node context is created at the time of each browser context creation and running in a same context as browser context, a.k.a. the Mixed context.

Comparing with Separate Context

The advantage of Separate Context Mode is that you will not encounter many type checking issue as below.

The cons is that in Mixed Context Mode, you can’t share variable easily as before. To share variables among contexts, you should put variables in a common context that can be accessed from the contexts you want to share with. Or you can use window.postMessage() API to send and receive messages between contexts.

再次回看nwjs的文档,很多概念有了更新的理解。对网站文档的印象也比之前大为改善。_

http://blog.csdn.net/practicer2015/article/details/78505287

相关文章

  • NWJS温故而知新

    去年基于NW.js 0.12.3做过一个项目,当时对于NW中的contexts的理解比较模糊。今天在twiiter...

  • NWJs

    首先和读者说一声抱歉,我的语文是体育老师教的,所以本文全是大白话,虽然看完了不会觉得养眼,但我相信有我这样的描述,...

  • nw.js包管理

    npm i -g nwjs usage

  • nwjs 启动一个vue项目

    nwjs 如何启用一个打包好的vue 项目 下载 nwjshttps://nwjs.io/ 官网下载一般情况下...

  • nwjs打包

    前端导航项目基于nodejs+vue开发,使用electron打包成exe进行安装到大屏运行,在模拟导航过程中会感...

  • nwjs学习(1)

    NW.js是什么?NW.js(原名node-webkit)是一个基于chromium和nodejs的应用运行时,通...

  • 您的个人资料来自更高版本的NW.js,因此无法使用。某些功能可能

    Mac下 进入目录 ~/Library/Application Support删除nwjs目录,每次打开nw软件都...

  • nwjs 开发以及打包

    坑集中营 0.13版本的分normal,sdk和XXX,只有sdk的可以通过按F12进入控制台。 0.12版本的w...

  • 使用NW.js搭建简单的webapp

    简介: nwjs是NodeJS的一个框架,也叫nodeWebkit,基于Chromium和NodeJS运行。 适用...

  • 2019-03-06

    电风扇的股份 ## 7.0.0 ### 7.0.0 新功能 1. 升级底层 nwjs 到 0.36.1 版本 2....

网友评论

    本文标题:NWJS温故而知新

    本文链接:https://www.haomeiwen.com/subject/risfaxtx.html