Note that this code will need to be changed when https://github.com/openlayers/ol3/pull/870 is merged.