The $toolbar variable is declared outside the scope of Drupal.behaviors.toolbar. The value is assigned in this line of code.
$toolbar = $(context).find('#toolbar-administration').once('toolbar');
On the first pass through attaching behaviors, $toolbar gets assigned a jQuery object with one element. On any subsequent pass on the save page, such as after an Ajax request, $toolbar is set to an empty jQuery object because the .once() method will on select anything on a second pass.

We need to update the code so that $toolbar is assigned only on the first pass through the attaching of behaviors.
Comments
Comment #1
David_Rothstein commentedMarking #1872274: Editing breaks the toolbar as duplicate and moving to critical, since this really causes the toolbar to break.
Simple way to reproduce is to go to admin/structure/views/add and change the view from Content to Users - that kicks off an Ajax request which triggers this.
Here's a patch along the lines of above. Somehow the whole code flow seems a little odd, but I guess it's OK (since $toolbar is a global variable we must be assuming that there is only one of them on the page, but we still look for it again every Ajax request on the off chance that the Ajax request caused the existing toolbar to be destroyed and a new one to be added?)
Comment #2
David_Rothstein commentedComment #3
damien tournoud commentedThe patch makes sense. Let's get this in.
Comment #4
jessebeach commentedI'd like to change the wording just a tad in the comment in this patch. In this case
$toolbaris not a global variable; it is referenced by the closure of the functionDrupal.behaviors.toolbar.attachand $toolbar is scoped to the anonymous immediately invoked function expression that surrounds all of the code in this file. It's true that this code assumes one toolbar on the page, which is why it is selected by an id in$(context).find('#toolbar-administration').once('toolbar'), not a class. This patch doesn't change the behavior of the fix, just the name of the$newToolbarvariable to$onceCheckand adjusts the comment accordingly.Comment #5
webchickCommitted and pushed to 8.x. Thanks!
Comment #6
David_Rothstein commentedCan we go back to something more like the original variable name? The thing that actually gets stored in this variable is a toolbar object, so calling it "onceCheck" doesn't sound right. I mean, lines like this just look really odd to me:
You're right about the code comment, though. It isn't a global variable (I forgot about the mega-function that wraps everything in Drupal JavaScript).
Comment #7
David_Rothstein commentedSomething like this, perhaps. I couldn't come up with anything better than $newToolbar, but pretty much anything that has the word "toolbar" in it works for me.
Comment #8
jessebeach commentedThe only reason that the
$onceCheckvariable exists is to check to see ifonce()has run on the toolbar.What about
$toolbarOnce?Comment #9
David_Rothstein commentedSure, why not.
Comment #10
jessebeach commentedChanged
$onceCheckto$toolbarOnce.Comment #12
wim leers#10: 1858576_toolbar-var_10.patch queued for re-testing.
Comment #13
David_Rothstein commentedFixed the code comment also.
Comment #14
tstoecklerThat definitely makes it more readable. Great!
Comment #15
webchickCommitted and pushed to 8.x. Thanks!