doulan8152 2012-12-19 13:33
浏览 81
已采纳

有没有办法在我的PHP MVVM设计中使用jQuery?

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条回答 默认 最新

  • douleijiang8111 2012-12-19 13:44
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置