0

我有一个模型叫游戏。在路由器中我这样做:

            var game = new models.Game({game_id:game_id,team_id:team_id});
            game.fetch({
              success: function (data) {
                    slider.slidePage(new Game({model:data}).$el);
                }
           });

然后在 Game 视图中,我想确定数据库是否返回任何内容。最好的方法是什么?目前我有:

    initialize: function (options) {
       if(this.model){
           console.log('in the if');
       }
       else{
           console.log('in the else');
       }
    }

但这将始终进入 if 即使没有从数据库返回任何行...

4

1 回答 1

0

也许您想检查id属性,理论上只有服务器应该知道。(或者,使用 . 检查名称等必需属性Model.has("game_name")。)

if(typeof this.model.id != "undefined") {
       console.log('in the if');
   }

或者,您可能要考虑让服务器返回类似“404 not found”的 HTTP 代码,以表示没有数据。这样,您可以确定success回调将始终有数据,并在回调中处理无数据情况error

于 2013-10-04T18:36:27.893 回答