JavaScript for WordPress Forums Vanilla JavaScript 1.6.10 View Menu Reply To: 1.6.10 View Menu

#22074
Zac Gordon
Keymaster

Hi Carme!

This is a really good question.

The reason the createMenuItem function went in the helpers file is because it really just server to create HTML markup. It is tied to the menu data, but really it should be written (and maybe even named) to be agnostic to whatever data is passed to it. Really it just creates link or menu markup.

Technically though since this code has to do with displaying the data, if it were to go somewhere else it would belong in the view. The view contains all the code for what is displayed in the browser. Since this code focuses on markup it could be argued it belongs in the view. The reason I didn’t put it there is that in the future we won’t need to write code like this thanks to libraries. This helper file is meant to mimic a library in that case so the code for creating markup was pulled out of the view.

You made a really good point though that this code has to do with the data so it should go in the model. Code dealing with data that you will see in the model is more like Get Post X or Set Post X or GetAllPosts and things like that. This model code creates collections or returns individual pieces of data.

View code also deals with data. It deals with displaying data (data that we get from our models). Since these functions deal with displaying data they technically belong to the view. Since this code for DOM creation can be extracted into it’s own helper “library” it was placed in the helper file.

Hope this helps explain things better! Again, a really good question!!!