Qt Quick Underscore.js: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
=Using Underscore.js library in Qt Quick=
h1. Using Underscore.js library in Qt Quick
 
<blockquote>
 
Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects.<br /> (http://underscorejs.org/)


<blockquote>Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects.<br />(http://underscorejs.org/)
</blockquote>
</blockquote>
 
Unfortunately you cannot just import Underscore like:<br /><code>import &quot;underscore.js&amp;quot; as Underscore<code><br />due &quot;immutable root object&amp;quot;:http://doc.qt.io/qt-5/qtqml-javascript-hostenvironment.html#javascript-environment-restrictions
Unfortunately you cannot just import Underscore like: <br /> due [http://doc.qt.io/qt-5/qtqml-javascript-hostenvironment.html#javascript-environment-restrictions immutable root object] ''[qt.io]''


The workaround is to modify ''underscore.js'' so that it returned underscore object via special ''init'' function.
The workaround is to modify ''underscore.js'' so that it returned underscore object via special ''init'' function.


This is a diff output for Underscore.js version 1.4.4:<br />
This is a diff output for Underscore.js version 1.4.4:<br /></code><br />1,2c1,4<br />&lt; // Underscore.js 1.4.4<br />&lt; // =
---
&amp;gt; //    Underscore.js 1.4.4
&amp;gt; //    http://underscorejs.org
&amp;gt; //    (c) 2009-2013 Jeremy Ashkenas, DocumentCloud Inc.
&amp;gt; //    Underscore may be freely distributed under the MIT license.
4,10c6,9
&amp;lt; // &amp;gt; http://underscorejs.org
&amp;lt; // &amp;gt; (c) 2009-2013 Jeremy Ashkenas, DocumentCloud Inc.
&amp;lt; // &amp;gt; Underscore may be freely distributed under the MIT license.
&amp;lt;
&amp;lt; // Baseline setup
&amp;lt; // --------------
&amp;lt; var init = function() &amp;#123;
---
&amp;gt; (function() &amp;#123;
&amp;gt;
&amp;gt;  // Baseline setup
&amp;gt;  // --------------
58,65c57,64
&amp;lt; //  if (typeof exports ! 'undefined') {<br />&lt; // if (typeof module ! &amp;#39;undefined&amp;#39; &amp;amp;&amp;amp; module.exports) &amp;#123;
&amp;lt; //      exports = module.exports = _;
&amp;lt; //    &amp;#125;
&amp;lt; //    exports._ = _;
&amp;lt; //  &amp;#125; else &amp;#123;
&amp;lt; //    root._ = _;
&amp;lt; //  &amp;#125;
---
&amp;gt;  if (typeof exports ! 'undefined') {<br />&gt; if (typeof module !== 'undefined' &amp;&amp; module.exports) {<br />&gt; exports = module.exports = '';<br />&gt; }<br />&gt; exports.'' = '';<br />&gt; } else {<br />&gt; root.'' = '';<br />&gt; }<br />1227,1228c1226<br />&lt; return'';<br />&lt; }<br />—<br />&gt; }).call(this);<br /><code>


Now you can use it in your project:<br />
Now you can use it in your project:<br /></code><br />import &quot;underscore.js&amp;quot; as Underscore<br />// …<br />property var _: Underscore.init();<br /><code>


Drawback of this approach is the following warning in application console:<br />
Drawback of this approach is the following warning in application console:

Revision as of 10:55, 24 February 2015

h1. Using Underscore.js library in Qt Quick

Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects.
(http://underscorejs.org/)

Unfortunately you cannot just import Underscore like:

import &quot;underscore.js&amp;quot; as Underscore<code><br />due &quot;immutable root object&amp;quot;:http://doc.qt.io/qt-5/qtqml-javascript-hostenvironment.html#javascript-environment-restrictions

The workaround is to modify ''underscore.js'' so that it returned underscore object via special ''init'' function.

This is a diff output for Underscore.js version 1.4.4:<br />


1,2c1,4
< // Underscore.js 1.4.4
< // =

--- &gt; // Underscore.js 1.4.4 &gt; // http://underscorejs.org &gt; // (c) 2009-2013 Jeremy Ashkenas, DocumentCloud Inc. &gt; // Underscore may be freely distributed under the MIT license. 4,10c6,9 &lt; // &gt; http://underscorejs.org &lt; // &gt; (c) 2009-2013 Jeremy Ashkenas, DocumentCloud Inc. &lt; // &gt; Underscore may be freely distributed under the MIT license. &lt; &lt; // Baseline setup &lt; // -------------- &lt; var init = function() &#123; --- &gt; (function() &#123; &gt; &gt; // Baseline setup &gt; // -------------- 58,65c57,64 &lt; // if (typeof exports ! 'undefined') {
< // if (typeof module ! &#39;undefined&#39; &amp;&amp; module.exports) &#123; &lt; // exports = module.exports = _; &lt; // &#125; &lt; // exports._ = _; &lt; // &#125; else &#123; &lt; // root._ = _; &lt; // &#125; ---

&gt; if (typeof exports ! 'undefined') {
> if (typeof module !== 'undefined' && module.exports) {
> exports = module.exports = ;
> }
> exports.
= ;
> } else {
> root.
= ;
> }
1227,1228c1226
< return
;
< }

> }).call(this);

Now you can use it in your project:<br />


import "underscore.js&quot; as Underscore
// …
property var _: Underscore.init();

Drawback of this approach is the following warning in application console: