1

This is a code that is repeating in my script couple of times so I would like to make it as function. I would like it to be the most basic function that can be. Also I would very much appriciate if some one would give me an example how can I call a function inside of my code. Thank you very much in advance!

Things that change through code are made bold (For future purposes: I am calling WFS layer from GeoServer with OpenLayers3):

    // format used to parse WFS GetFeature responses
    var **layer**GeojsonFormat = new ol.format.GeoJSON();

    // source
    var **layer**Source = new ol.source.Vector({
      loader: function(extent, resolution, projection) {
        var url = 'http://localhost:8080/geoserver/wfs?service=WFS&' +
            'version=1.1.0&request=GetFeature&typename=**ws**:**layer**&' +
            'outputFormat=text/javascript&format_options=callback:**layer**LoadFeatures' +
            '&srsname=EPSG:3765&bbox=' + extent.join(',') + ',EPSG:3765';
        // use jsonp: false to prevent jQuery from adding the "callback"
        // parameter to the URL
        $.ajax({url: url, dataType: 'jsonp', jsonp: false});
      },
      strategy: ol.loadingstrategy.bbox
    });

    /**
     * JSONP WFS callback function.
     * @param {Object} response The response object.
     */
    window.**layer**LoadFeatures = function(response) {
      **layer**Source.addFeatures(**layer**GeojsonFormat.readFeatures(response));
    };

   // layer
    var **layer**Layer = new ol.layer.Vector({
      source: **layer**Source,
      style: **layer**Style
    });

Later on, each layer must be included in a map, so I need to have layerLayer of var returned as well:

// map
var map = new ol.Map({
  target: document.getElementById('map'),
  renderer: 'canvas',
  view: olview,
  layers: [layerOneLayer, layerTwoLayer, layerThreeLayer, ...],
    logo: false
});
mycupoftea
  • 181
  • 1
  • 2
  • 8

2 Answers2

0

Wrap the code in a function and then call it like this - someFunction()

function someFunction() {
      // format used to parse WFS GetFeature responses
      var * * layer * * GeojsonFormat = new ol.format.GeoJSON();
  // source
  var * * layer * * Source = new ol.source.Vector({
    loader: function(extent, resolution, projection) {
      var url = 'http://localhost:8080/geoserver/wfs?service=WFS&' +
        'version=1.1.0&request=GetFeature&typename=**ws**:**layer**&' +
        'outputFormat=text/javascript&format_options=callback:**layer**LoadFeatures' +
        '&srsname=EPSG:3765&bbox=' + extent.join(',') + ',EPSG:3765';
      // use jsonp: false to prevent jQuery from adding the "callback"
      // parameter to the URL
      $.ajax({
        url: url,
        dataType: 'jsonp',
        jsonp: false
      });
    },
    strategy: ol.loadingstrategy.bbox
  });

  /**
   * JSONP WFS callback function.
   * @param {Object} response The response object.
   */
  window.* * layer * * LoadFeatures = function(response) { * * layer * * Source.addFeatures( * * layer * * GeojsonFormat.readFeatures(response));
  };

  // layer
  var * * layer * * Layer = new ol.layer.Vector({
    source: * * layer * * Source,
    style: * * layer * * Style
  });
}

someFunction()
marcusps1
  • 27
  • 7
0

You can make function with this.

function functionName(parameter1, parameter2) {
// your code
}
// call function
functionName();
Neutral Me
  • 107
  • 9