How can Gulp be restarted upon each Gulpfile change?

You can create a task that will gulp.watch for gulpfile.js and simply spawn another gulp child_process. var gulp = require(‘gulp’), argv = require(‘yargs’).argv, // for args parsing spawn = require(‘child_process’).spawn; gulp.task(‘log’, function() { console.log(‘CSSs has been changed’); }); gulp.task(‘watching-task’, function() { gulp.watch(‘*.css’, [‘log’]); }); gulp.task(‘auto-reload’, function() { var p; gulp.watch(‘gulpfile.js’, spawnChildren); spawnChildren(); function spawnChildren(e) { …

Read more

Add multiple window.onload events

Most of the “solutions” suggested are Microsoft-specific, or require bloated libraries. Here’s one good way. This works with W3C-compliant browsers and with Microsoft IE. if (window.addEventListener) // W3C standard { window.addEventListener(‘load’, myFunction, false); // NB **not** ‘onload’ } else if (window.attachEvent) // Microsoft { window.attachEvent(‘onload’, myFunction); }

React.js: Set a Default value into a prop

I believe that defaultProps should do what you need: import PropTypes from ‘prop-types’; class AppButton extends Component { render(){ return ( <button onClick={this.props.onClick}>{this.props.message}</button> ) } }; AppButton.propTypes = { message: PropTypes.string, onClick: PropTypes.func }; AppButton.defaultProps = { message: ‘Hello’, onClick: function(){ alert(“Hello”); } }; From the docs: The defaultProps will be used to ensure that …

Read more

How to build/concatenate strings in JavaScript?

With ES6, you can use Template strings: var username=”craig”; console.log(`hello ${username}`); ES5 and below: use the + operator var username=”craig”; var joined = ‘hello ‘ + username; String’s concat(..) var username=”craig”; var joined = ‘hello ‘.concat(username); Alternatively, use Array methods: join(..): var username=”craig”; var joined = [‘hello’, username].join(‘ ‘); Or even fancier, reduce(..) combined with …

Read more

How to open a file / browse dialog using javascript?

Here is a non-jQuery solution. Note you can’t just use .click() as some browsers do not support it. <script type=”text/javascript”> function performClick(elemId) { var elem = document.getElementById(elemId); if(elem && document.createEvent) { var evt = document.createEvent(“MouseEvents”); evt.initEvent(“click”, true, false); elem.dispatchEvent(evt); } } </script> <a href=”#” onclick=”performClick(‘theFile’);”>Open file dialog</a> <input type=”file” id=”theFile” />

How does one stub promise with sinon?

At current sinon version v2.3.1, you can use stub.resolves(value) and stub.rejects(value) function For example, you can stub myClass.myFunction with following code sinon.stub(myClass, ‘myFunction’).resolves(‘the value you want to return’); or sinon.stub(myClass, ‘myFunction’).rejects(‘the error information you want to return’);