2013-10-29 119 views
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) { 

回答

0

fetch執行異步HTTP(阿賈克斯)的要求,所以我passeed fetch一個成功回調,現在我看到的模態值。

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

我不知道你在哪裏,告訴你的代碼brandId獲取......您是否嘗試過這個?

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

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

-1

而且return '/rest/brands/' + brandId;大概應該是return '/rest/brands/' + this.get("brandId");

0

應該是:

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

然後,像@大衛提到:

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