almende / vis

⚠️ This project is not maintained anymore! Please go to https://github.com/visjs
7.85k stars 1.48k forks source link

allow Init function in the options object #4211

Open Granaat opened 5 years ago

Granaat commented 5 years ago

Allowing the function below in the options object would be a great help to use custom functions. It works atm but throws an error in the console.

const options = {
        Init: function() {
          this.Bind()
        },
        ...
}
mojoaxel commented 5 years ago

@Granaat Can you please provide more info. What are you trying to achieve?

Granaat commented 5 years ago

The objective is to be able to use "this" as the parent component in angular so I can bind to custom functions end component variables without passing callbacks or inline functions.

i.e.

manipulation: {
          enabled: false,
          addNode: (data, callback) => {
            this.actionService.addNode(data, callback)
          },
          editNode: (data, callback) => {
            this.actionService.addNode(data, callback)
          },
          addEdge: (data, callback) => {
            if (data.from == data.to) {
              var r = confirm("Do you want to connect the node to itself?");
              if (r == true) {
                this.actionService.addEdge(data, callback)
                // callback(data)
              }
            }
            else {
              this.actionService.addEdge(data, callback)
              // callback(data)
            }
            this.actionService.resetMode()
          },
          editEdge:  (data, callback) => {
            this.actionService.editEdge(data, callback)
          },
          deleteNode: (data, callback) => {
            this.actionService.deleteSelection(data, callback)
          },
          deleteEdge: (data, callback) => {
            this.actionService.deleteSelection(data, callback)
          },
...

I've bypassed the (typescript) error at the moment by ignoring the option 'init' in the errorcheck of the options class.