Could someone give me a basic example of how to share data between addViewRegistry and getView?

Basically, I want to pass data using Atom’s registerViewProviders() and getView().

I’ve been checking examples on Github … but their logic are too complex, so I got lost.

So far this is what I think I should do:

lib/package.js

'use babel';

import PackageView from './package-view';
import {CompositeDisposable, Disposable} from 'atom';

export default {

  subscriptions: null,

   activate(state) {
    this.subscriptions = new CompositeDisposable(
      atom.workspace.addOpener(uri => {
        if (uri === 'atom://sourchfetch') {
          this.registerViewProviders()
          return new PackageView();
        } 
      })
      ...
    );
  },

  registViewProviders () {
    const data = "Sample data"
    // register the data so it can be used in the view
    atom.views.RegisterViewProvider(???, function (model) {
      return ???
    }) 
  }
}

lib/package-view

'use babel';

export default class PackageView {

  constructor(serializedState) {
    this.element = document.createElement('div');
    this.element.classList.add('package');

    const message = document.createElement('div');
    message.classList.add('message');
    this.element.appendChild(message);
    // get the data to use it in the HTML
    const viewEl = atom.views.getView(???) 

    this.subscriptions = atom.workspace.getCenter().observeActivePaneItem(item => {
      if (!atom.workspace.isTextEditor(item)) return;
      message.innerHTML = `
        <p>${viewEl}</p>

I don’t know what to write in place of the ???'s above.

Can someone tell me how to complete this code? (Note: I’m not sure if the structure is right.)

Hello.

You are already in the implementation phase.
Could you please bring the forum up to speed on what you are trying to do.
What is the goal of your code.

Sharing data between two entities is not the goal - right?

My goal is to share data between the model (lib/package.js) and the view (lib/package-view.js) in an custom Atom package/plugin. But I can’t even figure out what are the correct arguments to give to addViewProvider and getView.

Without knowing the answer exactly…

A newly created visual element should be available to ‘read’ from, if that is required. Elements are added to the global document object, so it is globally available. But I think what you require is more about how JavaScript works, it is not about the API in Atom.

Creating an object in one script and having the another script require/include that is what you are looking for IMHO.

When working with Atom API it is because of objects and methods that are made available by including the ‘library’ to the user script. Should this concept not be applied to your application also: create objects?

1 Like

You are right. It was more of a JavaScript thing.

I wish I could delete this answer …

Don’t worry about it.


I see your discussion positively:

  • Creative problem solving

  • Researching

  • Team working:

    • detailing a suggestion

    • considering alternatives from others

    • giving feedback


Have yourself a good day.
1 Like