Qt Quick Underscore.js: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Convert ExpressionEngine links)
(clean-up)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
= 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.
<blockquote>Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects.
(http://underscorejs.org/)
(http://underscorejs.org/)
</blockquote>
</blockquote>
Unfortunately you cannot just import Underscore like:
Unfortunately you cannot just import Underscore like:
<code>import "underscore.js" as Underscore<code>
<code>import "underscore.js" as Underscore</code>
due [http://doc.qt.io/qt-5/qtqml-javascript-hostenvironment.html#javascript-environment-restrictions immutable root object]
due [http://doc.qt.io/qt-5/qtqml-javascript-hostenvironment.html#javascript-environment-restrictions immutable root object]


Line 13: Line 10:


This is a diff output for Underscore.js version 1.4.4:
This is a diff output for Underscore.js version 1.4.4:
</code>
<code>
1,2c1,4
1,2c1,4
< // Underscore.js 1.4.4
< // Underscore.js 1.4.4
Line 37: Line 34:
58,65c57,64
58,65c57,64
< //  if (typeof exports ! 'undefined') {
< //  if (typeof exports ! 'undefined') {
< // if (typeof module ! &amp;#39;undefined&amp;#39; &amp;amp;&amp;amp; module.exports) {
< // if (typeof module ! 'undefined' && module.exports) {
< //      exports = module.exports = _;
< //      exports = module.exports = _;
< //    }
< //    }
Line 46: Line 43:
---
---
>  if (typeof exports ! 'undefined') {
>  if (typeof exports ! 'undefined') {
> if (typeof module !== 'undefined' &amp;&amp; module.exports) {
> if (typeof module !== 'undefined' && module.exports) {
> exports = module.exports = '';
> exports = module.exports = '';
> }
> }
Line 58: Line 55:
> }).call(this);
> }).call(this);
<code>
</code>


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