ghettovoice / vuelayers

Web map Vue components with the power of OpenLayers
https://vuelayers.github.io/
MIT License
683 stars 229 forks source link

loader-factory communications #402

Closed wendellwt closed 3 years ago

wendellwt commented 3 years ago

Thank you for vuelayers; it is great! I have a question about vl-source-vector and :loader-factory.

I have a loaderFactory running fine, but can't get it to communicate with the rest of Vue. "this" seems to be undefined, and any arguments to my loaderFactory(vm) seem to be undefined also. I would like to do something like:

loaderFactory: (vm) => (extent, resolution, projection) => {
   let my_url = this.url_with_other_parameters;   // ERROR: TypeError: undefined has no properties
   return fetch(my_url)
      .then(response => response.json())
      .then(data =>  {
         let something_interesting = data.length;
         this.$root.$emit('new_data_arrived', (something_interesting) );  // ERROR: TypeError: undefined has no properties
         app.$emit('dlist', (dlist) );   // error: 'app' is not defined (no-undef) at src\components\AppOL.vue:124:1:
         return(data);
  }) }

How do I reference the data section or the $emit function? Thank you.

wendellwt commented 3 years ago

Nevermind. For whatever reason, this works:

 <vl-source-vector  ...  :loader-factory="loaderFactoryOuter"  />

    loaderFactoryOuter() {
      return (extent, resolution, projection) => this.loaderFactoryInner(extent, resolution, projection)
    },

    loaderFactoryInner(extent, resolution, projection) {
      return fetch(this.my_Url)
        .then(response => response.json())
        .then(data =>  {
         ...
          this.$root.$emit('mytag', (something) );
          return(data);
    })}
ghettovoice commented 3 years ago

Hello @wendellwt , in the first code sample you define loaderFactory with arrow function, so it don't have any this context by definition.

Second code sample is exactly how it is supposed to be used.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.