【问题标题】:Backbone model is not getting populated骨干模型未填充
【发布时间】:2013-11-08 16:13:25
【问题描述】:
app.Model.BrandModel = Backbone.Model.extend({
  idAttribute: 'brandId',
  defaults: {
    name : '',
    description : '',
    brandImage : '',
    user : '',
    showPro : false,
    proDescription : ''
  },
  url : function(){
    return '/rest/brands/' + brandId;
  }
 });

我可以从 firebug 看到我的服务器正在返回以下 JSON 响应。请求也成功。

brandId "fc692c70-4096-11e3-a0f2-3c970e02b4ec"
name "Galeria"
user "940ee800-4090-11e3-80c0-3c970e02b4ec"
description "This is galeria"
brandImage "/brand-images/fc692c70-...3-a0f2-3c970e02b4ec.jpg"
proDescription ""
showPro false

我是这样打电话的。

var brandModel = new app.Model.BrandModel();
brandModel.fetch();

但是我的模型没有被填充,并且值仍然是默认值。

我的控制器

@RequestMapping(value = "/rest/brands/{brandId}",
       method = RequestMethod.GET,
       produces = "application/json")
@ResponseBody
public Brand getBrand(@PathVariable("brandId") String brandId, HttpServletResponse response) {

【问题讨论】:

    标签: javascript jquery backbone.js


    【解决方案1】:

    我没有看到您在哪里告诉您的代码要获取哪个品牌 ID...您尝试过吗?

    var brandModel = new app.Model.BrandModel({brandId: "fc692c70-4096-11e3-a0f2-3c970e02b4ec"});
    brandModel.fetch();
    

    更多信息:How do I fetch a single model in Backbone?

    【讨论】:

      【解决方案2】:

      fetch 执行异步 HTTP (Ajax) 请求,因此我通过 fetch 成功回调,现在我看到了模态值。

      brandModel.fetch({
         success: function(){
             console.log(brandModel.toJSON());  
         }
      });
      

      【讨论】:

        【解决方案3】:

        应该是:

        app.Model.BrandModel = Backbone.Model.extend({
          idAttribute: 'brandId',
          urlRoot : '/rest/brands/',
          defaults: {
            name : '',
            description : '',
            brandImage : '',
            user : '',
            showPro : false,
            proDescription : ''
          }
         });
        

        然后就像@David 提到的:

        var brandModel = new app.Model.BrandModel({brandId: "fc692c70-4096-11e3-a0f2-3c970e02b4ec"});
        brandModel.fetch();
        

        【讨论】:

          【解决方案4】:

          还有return '/rest/brands/' + brandId;应该是return '/rest/brands/' + this.get("brandId");

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-03-04
            • 2013-10-14
            • 1970-01-01
            • 2014-06-25
            相关资源
            最近更新 更多