Backbone模型没有填充

时间:2013-10-29 13:19:31

标签: javascript jquery backbone.js

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 个答案:

答案 0 :(得分:0)

我看不到你在哪里告诉你的代码什么牌子要拿...你试过这个吗?

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

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

答案 1 :(得分:0)

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

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

答案 2 :(得分: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();

答案 3 :(得分:-1)

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

相关问题