2011-03-03 124 views
0

我有下面的代碼,它應該創建一個新的項目。代理類型是REST。Sencha Model.save發送/0.json到服務器

var inst = Ext.ModelMgr.create({ 
        title: values.title 
       }, "EntriesModel"); 

       inst.save({ 
        success: function(model) { 
         console.log(model); 
        } 
       }); 

後保存(),我看到,請求被髮送到http://localhost:3000/entries/0.json,而我認爲它應該已被送往http://localhost:3000/entries

項模型看起來像這樣

Ext.regModel("EntriesModel", { 
fields: [ 
    {name: "id",    type: "int"}, 
    {name: "title",   type: "string"}, 
    {name: "list_id",  type:"int"}, 
    {name: "bought",   type: "boolean"}, 
], 

proxy: { 
    type: 'rest', 
    url: '/entries', 
    format: 'json', 
    noCache: true, 
    reader: { 
     type: 'json', 
     root: 'data' 
    }, 
    writer: { 
     type: 'json' 
    }, 
    listeners: { 
     exception: function (proxy, response, operation) { 
      console.log(proxy, response, operation); 
     } 
    } 
} 
}); 

後端是Rails的。

回答

0

看看這個,如何建設一個休息代理鏈接:

buildUrl: function(request) { 
    var records = request.operation.records || [], 
     record = records[0], 
     format = this.format, 
     url  = request.url || this.url; 

    if (this.appendId && record) { 
     if (!url.match(/\/$/)) { 
      url += '/'; 
     } 

     url += record.getId(); 
    } 

    if (format) { 
     if (!url.match(/\.$/)) { 
      url += '.'; 
     } 

     url += format; 
    } 

    request.url = url; 

    return Ext.data.RestProxy.superclass.buildUrl.apply(this, arguments); 
} 

重寫此方法以提供更多的自定義,但記得要調用超類buildUrl

+0

我找到了其他解決方案。將id添加到Url由appendId屬性決定。我認爲,它不能將ID添加到創建方法中,因爲它在REST中沒有意義。但是,現在我在發送創建請求之前設置了appendId。 – 2011-03-03 19:50:26

0

嘗試讀取這個http://dev.sencha.com/deploy/touch/docs/?class=Ext.data.RestProxy 和示例:

new Ext.data.RestProxy({ 
    url: '/users', 
    format: 'json' 
}); 
// Collection url: /users.json 
// Instance url : /users/123.json 
+0

那麼,我已經閱讀文檔,並設置完全像你說的休息代理,而不是解決方案。 – 2011-03-03 19:48:19