我正在开发游戏应用程序并使用Symfony 2.0.我对后端有很多AJAX请求.更多的响应是将实体转换为JSON.例如:
class DefaultController extends Controller
{
public function launchAction()
{
$user = $this->getDoctrine()
->getRepository('UserBundle:User')
->find($id);
// encode user to json format
$userDataAsJson = $this->encodeUserDataToJson($user);
return array(
'userDataAsJson' => $userDataAsJson
);
}
private function encodeUserDataToJson(User $user)
{
$userData = array(
'id' => $user->getId(),
'profile' => array(
'nickname' => $user->getProfile()->getNickname()
)
);
$jsonEncoder = new JsonEncoder();
return $jsonEncoder->encode($userData, $format = 'json');
}
}
Run Code Online (Sandbox Code Playgroud)
我的所有控制器都做同样的事情:获取一个实体并将其一些字段编码为JSON.我知道我可以使用规范化器并对所有权限进行编码.但是,如果一个实体已经循环链接到其他实体呢?或实体图非常大?你有什么建议吗?
我想一下实体的一些编码模式......或者NormalizableInterface用来避免循环..,
我在使用jQuery插件tablesorter时遇到问题而且我无法调用触发器两次.
例如,这不起作用:
this._$table.trigger('update');
this._$table.trigger('sorton', [[[1,1]]]);
Run Code Online (Sandbox Code Playgroud)
但这有效:
this._$table.trigger('update');
setTimeout($.proxy(function() {
this._$table.trigger('sorton', [[[1,1]]]);
}, this), 1);
Run Code Online (Sandbox Code Playgroud)
然后我看到问题出现在触发器'update'中,它用body调用方法:
function () {
var me = this;
setTimeout(function () {
// rebuild parsers.
me.config.parsers = buildParserCache(
me, $headers);
// rebuild the cache map
cache = buildCache(me);
}, 1);
}
Run Code Online (Sandbox Code Playgroud)
为什么tablesorter开发人员使用setTimeout一毫秒?
我和朋友有争执.
他说我这个代码:
method SetBalance(balance) {
if (balance > 0) {
this.balance = balance;
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
比这更好:
method SetBalance(balance) {
if (balance < 0) {
throw new InvalidArgumentException("Balance couldn't be negative")
}
this.balance = balance;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:"哪种方法更适合验证?" 为什么?
谢谢.
ajax ×1
doctrine ×1
doctrine-orm ×1
javascript ×1
jquery ×1
oop ×1
settimeout ×1
symfony ×1
tablesorter ×1
validation ×1