Object State inside plugin

Wondered if anyone know if this is possible?

Lets say I update an object type of state with this…

         // update state
            if (update_selected_state) {
                instance.publishState('current_annotation', {
                    "_p_id": id,
                    "_p_name": "alpha",
                    "_p_comment": comment,
                    "_p_author": instance.data.annotation_1_author.trim()
                });
            }

And then later I decide I want to update it again, but ONLY one of those fields whilst keeping the other untouched. Can that be done?

I’ve tried a few ways and I thought sometime back I read somewhere that it could be done but it seems the whole object is required every time.

If I leave the ones I don’t want out of it entirely, like this:

         // update state
            if (update_selected_state) {
                instance.publishState('current_annotation', {
                    "_p_comment": comment
                });
            }

It clears the state entirely, so figured I’d ask the question.

@bubble would you be able to find out?

Cheers
Paul

Why not pass the old values with the new value

Yeh I thought of doing that, I just wondered if there was another way. It’s being run as part of an action so I guess I’ll just have to include a field which takes the state value as an input, then change and spit it out again.

Simple answer is no. You can’t ”update” a single field of a whole state. What you can do is republish the entire state, while changing only one field of the objects being published.

4 Likes

Great thx guys, I’ll pass it back in then :slight_smile:

1 Like

Why not store the entire value in an instance and use that to update the state. I don’t have my plugins docs handy but something like this.

instance.data.myState = {
_p_Text: “String”,
_p_Int: 1
}

Then from anywhere in the plugin you can update a value by doing
instance.data.myState._p_Text = “New String”

Then publish it

1 Like

That would work, why I didn’t think of that at the I’m not sure, thank you Ali.