Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give node id as per your choice #126

Closed
ishpreetkaurwebner opened this issue Mar 2, 2021 · 16 comments
Closed

Give node id as per your choice #126

ishpreetkaurwebner opened this issue Mar 2, 2021 · 16 comments
Assignees
Labels
question Further information is requested

Comments

@ishpreetkaurwebner
Copy link

In editor.addNode() function, we can pass name, input, output, position x, position y, class, and html. How we can pass node id and give node id according to our choice?

@jerosoler jerosoler self-assigned this Mar 2, 2021
@jerosoler jerosoler added the question Further information is requested label Mar 2, 2021
@jerosoler
Copy link
Owner

Hello @ishpreetkaurwebner

The node id cannot be passed.

It is self-assigned.

Only on import can you pass the id of the node.

Maybe you could change it with the "nodeCreated" event but it is not recommended.

Why do you need to know the id?

Jero

@pavlyuts
Copy link
Contributor

pavlyuts commented Mar 2, 2021

The key question is why you want to set IDs.

If your application need that node id be unique and never the same on different flows, you may set:

 editor.useuuid = true;

After that, all the IDs generated will be UUIDs instead of autoincrement integers, unique for any node created. However, if you copy one flow from other, it is your duty to re-asign all the IDs to got clear new instance.

@ishpreetkaurwebner
Copy link
Author

Is there anything like data-attr we can assign to node like we pass name and class to addNode()? I want to pass extra information, custom id we can't provide work according to you, so can we use our custom information on data-attr or any other suggestion you have?

@jerosoler
Copy link
Owner

You could create a custom html with a unique id for each node.

Or take the id assigned to it with the "nodeCreated" event.

Or create a hidden input with df- * variables and assign the variable to creation.

@pavlyuts
Copy link
Contributor

pavlyuts commented Mar 2, 2021

Or just add a property to a node and the property will survive export-import.

@ishpreetkaurwebner
Copy link
Author

Ok, thanks a lot, I will try

@rsvidevince
Copy link

rsvidevince commented Aug 19, 2021

I have a use case for this.

Im using Drawflow to render and edit some graph which is already saved in a database. Every node is a db row with self generated ids (primary keys), completely incompatible with the ids generated by drawflow...

A workaround would be to first create a new db row with a post, then import the data all over again, but that doesn't sound very smart (:

@jerosoler
Copy link
Owner

Hello @rsvidevince

Override the function uuid to get last insertdID in your db.

@rsvidevince
Copy link

Hmm, that sounds smarter! Can you give me a hint on how/where to do this ?

@jerosoler
Copy link
Owner

Use:

editor.useuuid = true;

editor.getUuid = function() {
/*
Your code for las insert db
*/
return your_last_inset_db;
}

@rsvidevince
Copy link

Thanks for your help @jerosoler !
I'm trying hard, but getUuid() always return before the async post, setting the id as undefined...
I tried with async/await and with .then(), axios and fetch, no juice at all.
Ideas?

@jerosoler
Copy link
Owner

jerosoler commented Aug 19, 2021

Mmmmmm

editor.getUuid = async function() { 
/* 
Your code for las insert db 
*/ 
return your_last_inset_db;
 }

¿?

@rsvidevince
Copy link

rsvidevince commented Aug 19, 2021

I tried :T

editor.getUuid = async function() {
  console.log('called getUuid()')
  let answer = await axios({
    method: 'POST',
    url: 'my-url-here'
  })

  console.log('got the answer', answer)
  return answer.data.id
}

Prints in the console like this:

called getUuid()
nodeCreated with id [object Promise] // this is a console.log on editor.on('nodeCreated')
got the answer { … }

Similarly:

        editor.getUuid = function() {
          console.log('getUuid')
          axios({
            method: 'POST',
            url: 'my-url'
          }).then(answer => {
            console.log('answer', answer)
            return answer.data.id
          })
        }

prints:

getUuid
nodeCreated with id undefined
answer {…}

@jerosoler
Copy link
Owner

Separe functions in two functions.
Example: https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Statements/async_function

@rsvidevince
Copy link

I tried that way too, unfortunately :T

        editor.getUuid = async function() {
          console.log('getUuid')
          let id = await resolveAfter2Seconds()
          console.log('awaited and got the', id)
          return id
        }

      function resolveAfter2Seconds() {
        return new Promise(resolve => {
          setTimeout(() => {
            resolve('resolved-id');
          }, 2000);
        });
      }

Prints:

getUuid
nodeCreated with id [object Promise]
awaited and got the resolved-id

I've changed my logic a little, and made it! I'm running editor.addNode() only after the post, than I can return the new id synchronously with getUuid().
Thanks for the help! This lib rocks!

@shamilyn
Copy link

I need to get the html elements of a particular node to update the values of that node and that node is separate component...
Is there any method to get it??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants