【问题标题】:How to check if a backbone model exists on the server如何检查服务器上是否存在主干模型
【发布时间】:2012-12-20 14:29:56
【问题描述】:

如果我这样做了

var model = new Model({ id : 'someNewId'});
model.fecth();

然后我得到一个具有默认值的模型,因为服务器上不存在具有“someNewId”概念的模型。

确保模型确实存在于服务器上的最佳方法是什么?

【问题讨论】:

    标签: backbone.js model


    【解决方案1】:

    这实际上取决于您的服务器配置。通常,RESTful 服务会返回 HTTP 错误代码 404 以指示未找到资源。

    model.fetch({
        success: function(model, response, options) {
            //model exists and is now populated
        },
    
        error: function(model, xhr, options) {
            if(xhr.status === 404) {
                //model was not found
            } 
            else {
                //some other error
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-12
      • 2012-08-03
      • 2012-10-01
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多