Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Qt Quick Underscore.js

From Qt Wiki
Jump to navigation Jump to search

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: