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?

图片转代码服务由CSDN问答提供 功能建议

一些背景:我来自.Net环境,我们必须在所有项目中使用MVVM来分离 从viewmodel查看。 现在我正在开发自己的个人PHP项目,我决定使用相同的设计并将视图和视图模型分开。

所以在视图中我创建了一个viewmodel对象并调用了后台函数 必要时在视图中代码仅用于提供显示。

示例:

view.php

 &lt;?php 
include('viewmodel.php'); 
 
 $ vm = new viewmodel.php(); 
 
if(某些条件)
 {
 $ vm-  &gt; doSomething(); 
} 
?&gt; 
&lt; html&gt; 
&lt; body&gt; 
 //某些表单代码
&lt; / body&gt; 
&lt; / html&gt; 
  <  /pre>
nn

viewmodel.php

<?php
//includes
nnclass viewmodel 
 {
 function viewmodel()  
 {
} 
 
函数doSomething()
 {
} 
} 
?&gt; 
   
 
 

现在我已经 学习jQuery我想用它来使我的页面更加动态,来回减少流量来回服务器,减少页面刷新的需要。

我看过这样的调用允许 jQuery用一些帖子数据调用一个php页面:

  $。ajax({url:'/ my / site',
 data:{action:'test'},
 type:'post',
 success:function(output){\  n alert(输出); 
} 
}); 
   
 
 

然而,这不再是面向对象设计,我将不得不从中删除所有类定义 我的viewmodels是为了工作。

有没有办法让jQuery对viewmodel进行调用或使用jQuery意味着你必须返回程序式的编程?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题