Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9008097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:51:44+00:00 2026-06-16T01:51:44+00:00

Some background: I came from a .Net environment where we had to use MVVM

  • 0

Some background: I came from a .Net environment where we had to use MVVM in all of our projects to separate the view from the viewmodel. Now that I’m working on my own personal PHP project I decided to use the same design and separate the view and viewmodel.

So on the view I create a viewmodel object and call background functions when necessary and in the view the code is only to provide the display.

Example:

view.php

<?php
include('viewmodel.php');

$vm = new viewmodel.php();

if(some condition)
{
  $vm->doSomething();
}
?>
<html>
<body>
//some form code
</body>
</html>

viewmodel.php

<?php
//includes

class viewmodel
{
  function viewmodel()
  {
  }

  function doSomething()
  {
  }
}
?>

Now that I’ve learned jQuery I want to use it to make my pages more dynamic, have less traffic back and forth to the server and less need for page refreshing.

I’ve seen calls like this that allow jQuery to call a php page with some some post data:

$.ajax({ url: '/my/site',
         data: {action: 'test'},
         type: 'post',
         success: function(output) {
                      alert(output);
                  }
});

However this is no longer Object Oriented Design and I would have to remove all the class definitions from my viewmodels for this to work.

Is there any way for jQuery to make those calls to the viewmodel or does using jQuery mean you have to return to a procedural style of programming?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:51:45+00:00Added an answer on June 16, 2026 at 1:51 am

    However this is no longer Object Oriented Design and I would have to remove all the class definitions from my viewmodels for this to work.

    What makes you think that? There’s nothing inherently object-oriented or non-object-oriented about making AJAX requests to the server. Nor do these client-originated requests have any bearing on the architecture of the server-side code.

    In your views you can have any JavaScript code you’d like. It won’t make a difference to the server-side code rendering the view, as the JavaScript is just text as far as it’s concerned and no different than the HTML.

    As for the server-side handler, what you’ll probably want to do is create entirely new views and viewmodels for the “page” (“resource” is a more accurate word) that’s being called by the jQuery code.

    So, for example, you might get a request from the browser for something like /my/site which would, using the architecture you have in place, populate a viewmodel to bind to a view and return that view to the client. That view would contain some JavaScript code, some of which calls another resource. Let’s say that other resource is something like /api/products which returns a list of “products” available for the user.

    Note that I put the resource under a different path, api instead of my. This was a pretty arbitrary decision, and it’s only used to illustrate that what you’re returning in this case isn’t a “page” so much as it is JSON data for an automated API. The actual name of the resource can be anything you like, of course.

    This resource can still be a view and be bound to a viewmodel, but in this case the view is JSON data instead of HTML. As with anything, there are many ways you can do this. A quick Google search turned up another question which talks about the differences between a couple of methods for generating JSON data. Indeed, your “view” in this case might be entirely empty and all you’re doing is serializing a viewmodel to JSON and returning that. (My PHP is pretty rusty, but the ASP.NET MVC equivalent would be to return an ActionResult of Json() instead of View().)

    Keep in mind that, from the perspective of the server-side code, there is no difference between returning a view in HTML vs. returning one in JSON (or XML, or raw text, or anything else). A view is a view, each one independent from the others.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came from a java background, and I was trying to use protocol like
Some background: I create a new ASP.NET MVC 3 WebApplication. Then I add a
Some Background to begin: I've implemented a custom MembershipProvider that validates a user from
I came from a Windows background whern it comes to development environments. I'm used
I have an activity that gets some data from the internet in background while
Background From the valuable advice I received here I have now moved all of
Coming from a Java/C# background, the MEL site came as a bit of a
First, some background: I've decided to start a new project designed from the ground
Background We have a Windows .NET application used by our field employees who travel
Background I have a Crystal Reports 2008 file that came my way from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.