2016-08-30 68 views
2

我有了主心骨模型這樣骨幹模型保存與非寧靜API

Report = Backbone.Model.extend({ 
    urlRoot: '/reports' 
}) 

我做了什麼:

model = new Report({id: 1, name: "Test Report1"}); 
model.urlRoot = "/reports/" + model.get('id') + "/submit"; 

model.save(null, { 
    patch: true, 
    success: function(model, res){ 
    console.log(res); 
    }, 
    error: function(err){ 
    console.log(err); 
    } 
}); 

但是當我保存它,我想這將請求發送到路徑像/reports/:id/submitpatch,但它會走到這樣的路徑/reports/1/submit/1POST。我能在這裏做什麼?任何調整?或者我應該使用$.ajax而不是?

+0

您的'urlRoot'屬性可以作爲'model.urlRoot =「/ reports /」傳遞''id'會自動附加。 –

+0

好的,但是如何將'/ submit'追加到'/ reports/1'? – Kumar

+0

當您將'urlRoot'設置爲'model.urlRoot =「/ reports /」' –

回答

0

覆蓋url()功能,而不是urlRoot

Url在函數/參數url中生成。

model = new Report({id: 1, name: "Test Report1"}); 
oldUrl = model.url; 
model.url = "/reports/" + model.get('id') + "/submit"; 

model.save(null, { 
    patch: true, 
    success: function(model, res){ 
    console.log(res); 
    }, 
    error: function(err){ 
    console.log(err); 
    } 
}); 

model.url = oldUrl;