doulan8152 2012-12-19 05: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 05: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 fluentmeshing
  • ¥15 手机/平板的浏览器里如何实现类似荧光笔的效果
  • ¥15 盘古气象大模型调用(python)
  • ¥15 传人记程序做的plc 485从机程序该如何写
  • ¥15 已知手指抓握过程中掌指关节、手指各关节和指尖每一帧的坐标,用贝塞尔曲线可以拟合手指抓握的运动轨迹吗?
  • ¥50 libwebsockets 如何添加其他socket事件回调
  • ¥50 实现画布拖拽算子排布,通过flink实现算子编排计算,请提供思路
  • ¥15 esium自定义材质拉伸问题
  • ¥15 cmake+mingw使用<mysqlx/xdevapi.h>报错
  • ¥15 eNSP中防火墙的使用
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部