PHP MVC with pure Javascript View: good practice?

My question may not be clear enough, so let me explain the situation:

I am working on a large server-side ajax webApp, with PHP using CodeIgniter. This structure clearly defines models, controllers, and views. Presentation files are displayed in HTML and then sent to the client, which processes some js procedures (for example, attaches events).

This way of working seems strange to me because it separates the view between the server side and the client side.

I was thinking of porting all View processing to the client part, which will dynamically build html in js. Then the server side will send only the raw data.

I worked this way on small projects, and I was very pleased with the result (easily understandable, portable and reusable).

Is it correct to implement an MVC application? Any tips around this reflection?

+3
source share
6 answers

I have done quite a lot of what you describe in a fairly large data services application as an internal application. In my case, I used ExtJS for client side rendering / views and contacted the C # WCF endpoint open on the web server. Essentially, they were made / presented, and the responses were serialized to / from JSON. It worked very smoothly as soon as some kinks were developed. The original author wrote his own serializer to directly execute direct results from their data level ... this leads to a lot of additional data going down the pipe. As long as you are smart with your payload data, this can be very effective.

Some reservations though ...

  • You should probably avoid this if you expect users who do not support javascript to be able to access the site (anything related to money transactions from external users).
  • You will want to document your methodology as clearly as possible.
  • Finding developers for maintenance tasks after you deploy the application will be very difficult. (Many server developers are shy, afraid, or simply ineffective with JS skills.

For the most part it is tossed, I believe that most people at least have JS, but can block other things. Support for AJAX / XmlHttpRequest is almost universal at the moment.

As for templates for displaying on the client side, there are several options (but this is a separate discussion).

+3
source

Building JavaScript views works great in the MVC template, as your view does not mix with your business logic or model.

However, there are several drawbacks to using full javascript views. This basically eliminates the possibility of elegant degradation if the client has javascript disabled. In addition, some browsers (IE) do not have a very fast JavaScript engine, which will make your page slower. It is true that part of the view is shared between the client and server, but it makes sense when you think about it.

In most cases, the HTML that you send to clients is the same for everyone (unless you discover a browser on the server side). However, JavaScript routines are different. If you use the jQuery library, this will be hidden from you, but the code that runs on each client can vary greatly. One example of this would be XMLHttpRequest, which is used by the firefox / webkit browser and the active x control, which is used by IE. Since the html part of the content has the same meaning for everyone, it makes sense to build on the server, and since the JavaScript viewing part may be different, it makes sense that it is built on the client side.

NTN

+2
source

I started using the same approach: JavaScript for the user interface level and PHP for the database access level. I use AJAX to transfer all data back and forth between two layers. Until now, AJAX froze from time to time, but in most cases it was fast enough. Therefore, I think it will work quite well.

(As a result, my code switched from 90% PHP with 10% JavaScript ... to 65% JavaScript with 35% PHP.)

I also separated the code for viewing my page from the code for my running event action functions. Therefore, I like to think that I now have an MVC layout (although I do not use ready-made MVC structures like Backbone.js).

I do not use HTML templates. I do not think it is natural that there is a 100% separation between HTML and programming. I think that simple programming loops, conditional statements, and JavaScript triggers all work well with HTML.

+2
source

If you think that there is a basic view that creates the html / js engine and a pair of ajax representations with data streams, this will be quite normal in the conditions of MVC imo.

0
source

Is there something more dynamic on the site itself? Does it do more AJAXy things, dynamically update parts of a site, etc.? If so, it might be wise to have only a Javascript site.

Since this is not the way the Internet traditionally works, sending HTML from the server is still basic. If your pages are mostly static, if you want to serve older clients, an audience that may disable Javascript, an audience that may have problems accessing only Javascript pages, alternative clients that Javascript or search engines cannot understand, you should serve HTML pages from the server. There is nothing wrong with that; it is straightforward, simple and reliable. There are many things to consider when reimagining a wheel in client-side Javascript. If you do not use the potential of this offer (see, for example, highly dynamic Facebook or Twitter pages), this may be more of a problem than for your users.

0
source

It looks like you are one step away from moving from an MVC template to an MVVM template.

MVVM is ideal for complex user interfaces (this is exactly what you would create with all AJAX and JavaScript, and much more), because in this case your HTML representation will be able to act as a controller through JavaScript. There is a library (warning: I never used it, but it looks promising) for this is called JS knockout .

0
source

Source: https://habr.com/ru/post/1344848/


All Articles