Qt Quick Underscore.js: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(clean-up)
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
h1. Using Underscore.js library in Qt Quick
= 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.
<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/)
(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:
<code>import "underscore.js" as Underscore</code>
due [http://doc.qt.io/qt-5/qtqml-javascript-hostenvironment.html#javascript-environment-restrictions immutable root object]


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


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>
Now you can use it in your project:
<code>
import "underscore.js" as Underscore
// …
property var _: Underscore.init();
</code>


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

Latest revision as of 01:25, 24 March 2016

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 "underscore.js" as Underscore

due immutable root object

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:

1,2c1,4
< // Underscore.js 1.4.4
< // =
---
> //     Underscore.js 1.4.4
> //     http://underscorejs.org
> //     (c) 2009-2013 Jeremy Ashkenas, DocumentCloud Inc.
> //     Underscore may be freely distributed under the MIT license.
4,10c6,9
< // > http://underscorejs.org
< // > (c) 2009-2013 Jeremy Ashkenas, DocumentCloud Inc.
< // > Underscore may be freely distributed under the MIT license.
< 
< // Baseline setup
< // --------------
< var init = function() {
---
> (function() {
> 
>   // Baseline setup
>   // --------------
58,65c57,64
< //  if (typeof exports ! 'undefined') {
< // if (typeof module ! 'undefined' && module.exports) {
< //      exports = module.exports = _;
< //    }
< //    exports._ = _;
< //  } else {
< //    root._ = _;
< //  }
---
>   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:

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

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