我几天前听说过MVVM,我搜索过它并阅读了一些文章,但我现在真的很困惑,而且我不知道Model-View-ViewModel中的ViewModel究竟是什么
查看: View是一个简单的(无逻辑)输出模板,就像Mustache模板一样:
Hi {{name}}!
型号:保存并检索原始数据。像用户类一样:
class user
{
public function __construct($username){...};
public function set_firstname($firstname){...};
public function get_firstname(){...};
public function set_lastname($lastname){...};
public function get_lastname(){...};
}
ViewModel:处理模型的原始数据并将结果发送到View
class ViewModel
{
public function request_showUserPage()
{
$user = new user('sample@example.com');
$firstname = $user->firstname;
$lastname = $user->lastname;
$name = $firstname . ' ' . $lastname;
$variables =
[
'name' => $name
]
$Mustache = new Mustache_Engine
echo $Mustache->render('{{name}}', $variables);
}
}
因此,输出将是:
Hi John Smith!