0
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) {
4

4 回答 4

0

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

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

更多信息:如何在 Backbone 中获取单个模型?

于 2013-10-29T13:28:42.980 回答
0

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

brandModel.fetch({
   success: function(){
       console.log(brandModel.toJSON());  
   }
});
于 2013-10-29T15:08:02.300 回答
0

应该:

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();
于 2013-10-30T06:43:55.017 回答
-1

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

于 2013-10-29T13:53:42.833 回答