我有一个骨干模型,我正在调用fetch。我有一个烧瓶服务器,我需要访问骨干模型的ID。我似乎无法抓住服务器上模型的ID。如何访问烧瓶代码中的entityId
BB.Politician = Backbone.Model.extend({
defaults: {
type: "Politician"
},
url: "/my_url_here"
});
var currentUser = new BB.Politician({"entityId": "1625"});
currentUser.fetch({
//method: "POST",
success: function(user){
currentUserView.render();
}
});
#Flask server code
@app.route('/my_url_here', methods=['GET', 'POST'])
def return_poitician():
print request
print request.args
print request.values
#none of the above print statements are giving me the "entityId"
return data
我也尝试在路由中添加id但是在执行fetch()
时只是抛出了404错误:
@app.route('/my_url_here/<entityId>', methods=['GET', 'POST'])
def return_poitician(entityId):
print entityId
答案 0 :(得分:2)
@app.route('/my_url_here/<entityId>', methods=['GET', 'POST'])
没有提取任何id
,因为您没有发送任何内容。
Backbone fetch使用模型的id
字段来构建获取URL,在您的情况下,我建议将entityId
转换为id
:
BB.Politician = Backbone.Model.extend({
defaults: {
type: "Politician"
},
url: "/my_url_here"
});
var currentUser = new BB.Politician({"id": "1625"});
让Backbone构建GET,它看起来像:
"/my_url_here/" + this.get('id'); // this refers to model
变成
"/my_url_here/1625"
Backbone.Model.url
也接受函数作为值,因此您可以定义自己的构造URL的逻辑。例如,如果您必须保留entityId
,则可以构建url
之类的内容:
url: function () {
return "/my_url_here" + this.get('entityId');
}