simplemde-markdown-editor icon indicating copy to clipboard operation
simplemde-markdown-editor copied to clipboard

Duplicate initializations should be prevented

Open ArtOfCode- opened this issue 9 years ago • 8 comments

This is an odd one. I've implemented SimpleMDE for a development website. On the page where it's used (http://stage.artofcode.co.uk:8800/recipes/new - requires sign in), when I reload the page, the editor duplicates.

We go from this on the first page load:

to this on the reload:

My initialization code is this:

$('.mde-field').each(() ->
  mde = new SimpleMDE({
    element: this,
    forceSync: true,
    indentWithTabs: false,
    tabSize: 4
  })
)

Am I doing something wrong, or is this a bug?

ArtOfCode- avatar Aug 01 '16 19:08 ArtOfCode-

I have a feeling this is a bug not related to SimpleMDE, but rather, something related to your application code.

WesCossick avatar Aug 01 '16 21:08 WesCossick

@WesCossick indeed but that is also a sign that simplemde instances are not indempotent. Is it something you can fix?

borisrorsvort avatar Sep 01 '16 06:09 borisrorsvort

The instances should be idempotent. I'm relatively certain the bug has nothing to do with SimpleMDE's code. But, I'd be curious to see if a JSFiddle can be put together showing that this assumption is incorrect. Until, I'm going to mark this issue as closed.

WesCossick avatar Sep 04 '16 00:09 WesCossick

@WesCossick Here you go http://jsbin.com/saxecumere/edit?html,js,output Expected behavior is that it does't get instanciated more than once per textarea. Here it does, hence it's not idempotent ;)

borisrorsvort avatar Sep 04 '16 08:09 borisrorsvort

Looks like the bug on my end is that the JS is running twice, for some reason which I have yet to work out. That said, @borisrorsvort is correct - it would make sense for SimpleMDE to reject initialization attempts on elements that have already had SimpleMDE initialized on them.

ArtOfCode- avatar Sep 04 '16 12:09 ArtOfCode-

@ArtOfCode- It's probably because your using turbolinks and the init code get cached then re-executed again (https://github.com/turbolinks/turbolinks#responding-to-page-updates). But that is not a problem if the code is idempotent, hence my comment

borisrorsvort avatar Sep 04 '16 13:09 borisrorsvort

@WesCossick :+1:

borisrorsvort avatar Sep 04 '16 19:09 borisrorsvort

Well, there is another layer of this. If you call new SimpleMDE() (without target element) and then finally you call it again (with target element) it will duplicate the editor, too 😄

   new SimpleMDE()
   new SimpleMDE()
   new SimpleMDE({ element: document.getElementByid("MyId") })

We will get 3 editors on MyId 🤣

ktrzeciaknubisa avatar Mar 21 '23 20:03 ktrzeciaknubisa