NWJS溫故而知新

去年基于NW.js 0.12.3做過一個項目,當時對于NW中的contexts的理解比較模糊。今天在twiiter上看到發(fā)布新的版本,重新又打開了nwjs.io網(wǎng)站的文檔部分,落地頁面是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的文檔,很多概念有了更新的理解。對網(wǎng)站文檔的印象也比之前大為改善。_

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,159評論 0 23
  • 信息時代,我們的生活發(fā)生天翻地覆的變化,在20年前電腦還沒有普及,老百姓都是看看電視和收音機度日。記得那個時候最喜...
    九魚亭閱讀 342評論 0 2
  • 《生活周期》 我的生活的周期,就是我的思念的周期。因為我的全部的生活,都洋溢著最真摯的思念的律動,時時刻刻,分分秒...
    長島冰淚Willin閱讀 490評論 0 0
  • 1.如果再回到從前,我想成為畫家! 我會支起開裂的畫架,繃上泛黃的畫布 拾起那早已干硬的畫筆,暈開干涸的油彩——我...
    GreatWall_Z閱讀 549評論 2 7
  • 有一次茉莉去參加家庭的一場喜宴,她穿著一身黑色休閑的衣服去了,在客廳里等待入席的時候,看到來參加喜宴的女客人,...
    梅韻Eva閱讀 233評論 0 2

友情鏈接更多精彩內容