2

I am coding a cesium app and I would like to call an internal cesium function that clears all primitives:

function clearAll() {
    primitives.removeAll();
}

when i do a button press. I know about cesium's built in toolbar buttons but I would like to use an html button I already have in place to call this function on click. It's located in:

<script>
require(['Cesium'], function(Cesium) {
    function clearAll() {
        //code here}
    });
</script>

Any help would be appreciated!

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Doug
  • 53
  • 1
  • 6
  • No. If it's internal (and not exported) it will stay internal (private) until you change the library code. – Bergi Jul 09 '14 at 18:17
  • Do you know how I would make the function public so I can access it? – Doug Jul 09 '14 at 18:19
  • Where do you want to access it? Where did you put your code if not in that `require` callback? – Bergi Jul 09 '14 at 18:21
  • @Bergi I want to use a button in my html body like this to call the function because I want to be able to alter my script without having to reload the whole script. – Doug Jul 09 '14 at 18:26
  • No. Just don't do this. Don't use inline event handlers. Please. – Bergi Jul 09 '14 at 18:27
  • @Bergi Any reason why this is bad practice? – Doug Jul 09 '14 at 18:58
  • You could have done that search yourself, couldn't you? http://stackoverflow.com/q/5871640/1048572 http://stackoverflow.com/q/15792498/1048572 – Bergi Jul 09 '14 at 19:02

1 Answers1

2

Take a look at the top of our Billboards example. When Cesium.Viewer is constructed, it provides access to viewer.scene.primitives which includes the .removeAll() function, which is public.

var viewer = new Cesium.Viewer('cesiumContainer');
var scene = viewer.scene;
var primitives = scene.primitives;

function reset() {
    primitives.removeAll();
}

As @Bergi mentions in the comments, typically you don't wire this up with onclick because you need access to scoped variables like the viewer instance. Instead, give your button an id attribute, and use addEventListener (or jQuery) to listen for the button click within that scope.

document.getElementById('myButtonId').addEventListener('click', function() {
    primitives.removeAll();
}, false);
emackey
  • 11,818
  • 2
  • 38
  • 58