小编Dmy*_*sun的帖子

如何在Symfony 2.0 AJAX应用程序中将Doctrine实体编码为JSON?

我正在开发游戏应用程序并使用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用来避免循环..,

ajax doctrine symfony doctrine-orm

87
推荐指数
10
解决办法
11万
查看次数

为什么一些JavaScript开发人员使用setTimeout一毫秒?

我在使用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一毫秒?

javascript jquery tablesorter settimeout

12
推荐指数
2
解决办法
5248
查看次数

在二传手中验证:哪种方法更好?

我和朋友有争执.

他说我这个代码:

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)

我的问题是:"哪种方法更适合验证?" 为什么?

谢谢.

oop validation design-patterns

4
推荐指数
1
解决办法
226
查看次数