如何访问Backbone Fetch数据服务器端
How to access Backbone Fetch data server side
我有一个主干模型,用它调用fetch。我有一个flask服务器,我需要在它上访问主干模型的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
@app.route('/my_url_here/<entityId>', methods=['GET', 'POST'])
没有收到任何id
,因为您没有发送任何。
Backbone fetch使用模型的id
字段来构建fetch 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');
}
相关文章:
- Backbone fetch中的Ajax在fetch调用退出后完成,因此fetch调用中没有成功/失败事件
- Backbone.js Collection fetch()抛出未捕获类型错误:无法读取属性'idAttribu
- fetch()不调用其Success和Error回调函数(backbone.js)
- backbone.js fetch()返回对象而不是子对象
- 在 Backbone 中,为什么我在执行 fetch() 时无法触发“重置”事件
- Backbone.Fetch 方法应该在集合中还是视图中
- Backbone fetch返回我的模型的所有实例
- Backbone fetch,promise and if else.它们是如何工作的
- Backbone JS Model Fetch在Fiddler中返回200,但调用错误方法
- Backbone.js fetch()没有更改URL
- Jquery Promise Backbone Fetch
- 如何在使用Backbone'进行AJAX请求时访问XHR对象;s Fetch()
- 为什么'fetch函数'自动调用'解析函数'在backbone.js集合上
- 如何通过fetch获取添加到Backbone集合的项目的索引
- Backbone.js fetch()大型集合会导致脚本冻结
- backbone.js collection-重复调用fetch以获取所有“;页面”;来自服务器
- backbone.js集合-如何为fetch定义成功回调(不过是递归的)
- Backbone Fetch中的mock JSON响应
- 如何访问Backbone Fetch数据服务器端
- Backbone.fetch()清理数据重写