jeudi 22 octobre 2015

MVC Best practice Partial View / JavaScript

I searched the internet and really can't find a good answer to this question.

Imagine there is a View that contains a simple tab-element. Each tab-content is loaded dynamically (via ajax) when the tabs header is clicked. The View itself has the document.ready-handler to do things when the View (not the partial) is loaded. (e.g. init the Views javascript module).

One partial contains a list of elements which can be deleted. Clicking the Delete button should open a modal dialog to let the user confirm the deletion.

What would be the best way to handle this?

The View does not know about the Partial which has been loaded and vice versa.

The View has some kind of module which will have a function

deleteElement(id){ //... }

How should this be called from within the partial?

Should the partial know about the deleteElement? What if it is called inside another View which module does not have such a function?

Should the deleteElement be declared global? Does this make sense when only needed in some specific context?

Or is the whole idea of splitting into single javascript-modules per View already the wrong decision I've made? (My thoughts was to keep a single .js file which is only loaded when really needed, keep code clean, separation of concerns, ...)

Any links, hints, workarounds or ideas would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire