• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HTML in Model

#1
Hi Everyone

New to MVC Model programming, so basic question sorry : 

Normally, if I understand correctly, this is where everything resides : 

Controller : Loads the different logic and views
Model : contains the logic (ie loading data from MySQl, different functions for calculating, etcetera)
View : Contains the HTML markup and little PHP to ech values received from the Model

This is all pretty straightforward until you run into AJAX to recalculate certain parts of your page.
Let's say I have a table with users, when I add a user I want to reflect that in the table without refreshing the whole page.
So normally you would run an AJAX call to some PHP script that then renders the table again.

Under the MVC model, my AJAX call is done to model/function , but that effectively means that part of my HTML markup
will have to be moved to the Model, because the return value to the AJAX call has to contain all the HTML in order to put it in the response DIV . And this is my question : is it normal and OK to move parts of HTML to the model

Thanks, hopefully it's a bit clear what I'm trying to explain.
Reply

#2
Noooooooooo - don't do it!
What you are looking for would be to have the AJAX request expect JSON data back from the server-side controller, which referenced the appropriate model. You would then format/mangle/render that JSON data client-side.
So, no it is not normal & ok, IMHO. Your original (bolded) perspective makes the most sense!
Reply

#3
I agree, and it was my gut feeling that no HTML should be in model .... unfortunately I have no idea where to start since when I call the model/function , it will return only JSON data, then where do I get my formatting from.

Normally when you run an AJAX call you have the "success" handler in which you just print the result like $('#mydiv').html(data) where "data" contains the full rendered HTML with values , but since I can't use HTML in the model , where do I get that from then.

Sorry, i'm totaly confused
Reply

#4
(09-25-2019, 11:35 PM)AngelTrader Wrote: I agree, and it was my gut feeling that no HTML should be in model .... unfortunately I have no idea where to start since when I call the model/function , it will return only JSON data, then where do I get my formatting from.

Normally when you run an AJAX call you have the "success" handler in which you just print the result like $('#mydiv').html(data) where "data" contains the full rendered HTML with values , but since I can't use HTML in the model , where do I get that from then.

Sorry, i'm totaly confused

Most often the AJAX is returning data to an already existing page of HTML and the success function uses the return to modify what's there. So the answer to "where do I get my formatting from" is that it's either already there, or the success function will create it by concatenating string literals and the returned data before injecting the results into the DOM.

It's not true that you "can't use HTML". You most certainly can, but you should adjust the $.ajax options accordingly. AJAX is seen as a way to offload presentation work to the client-side and JSON make great sense for the data format. But sometimes is just plain easier to send back HTML markup that's ready for display.

You can make $this->load->view('something') to return blocks of HTML to ajax.success. Read the docs again looking for the example on how to have view() return a string instead of outputting directly.
Reply

#5
NOP

Models should contain business logic that is related to the DB table the model is associated with

Controllers should get data from models and/or libraries and pass them to the view

Views should have HTML code.

HTML code should never be in your controllers MUCH LESS your models.
Codeigniter is simply one of the tools you need to learn to be a successful developer. Always add more tools to your coding arsenal!
Reply

#6
I am not thinking HTML in any controller, but having the controller build view fragments to return.
I think we are on the same page,
Reply

#7
I've created a seperate view for the table structure.
I then created a seperate function in the controller that loads the data in the view and returns it as a string (byu appending 'true' at the load view function).

I then return the full HTML string back to the AJAX success function.

Works like a charm, thank you everyone !!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.