问题描述
在骨干网中,如何将我的实体指向外部端点?
In backbone, how can I point my entities to an external endpoint?
例如,我的应用程序在 http://myapp.com
For example, my app is running on http://myapp.com
我希望它使用followgin rest Web服务
And I want it to use the followgin rest web service
http://external.com/api/rest/xxxx
我尝试使用 urlRoot 属性,但是它似乎无法正常工作
I tried with urlRoot property but it doesn't seem to work that way
Sagan.FeatureModel = Backbone.Model.extend({
defaults: {
name: "New Feature",
parent: "",
enabled: false
},
urlRoot: 'http://localhost:9001/',
url: 'features'
});
出于测试目的,该应用程序托管在localhost:9000上,外部Web服务托管在localhost:9001.
For testing purposes the app is hosted on localhost:9000, and the external webservice at localhost:9001.
主干似乎仍然指向localhost:9000而不是9001
backbone seems to still be pointing at localhost:9000 instead of 9001
推荐答案
在您的示例中,您为模型设置了海关urlRoot
和url
属性.
In your example you are setting customs urlRoot
and url
properties for the Model.
设置自定义url
属性将使您的自定义urlRoot
被忽略,因为此属性用于 default url
行为,请查看 Model.url文档.
Setting up custom url
property will make your custom urlRoot
to be ignored due this property is used in the default url
behavior, look on the Model.url documentation.
如果您希望模型使用端点 http://external.com/api/rest/features
,只需将其添加到urlRoot
并保持url
不变:
If you want your Model to use the endpoint http://external.com/api/rest/features
just add it to the urlRoot
and keep the url
untouched:
urlRoot: "http://external.com/api/rest/features"
它将创建如下路线:
GET http://external.com/api/rest/features/1
用于获取ID为1的模型.
For fetching the Model with id 1.
这篇关于设置骨干指向外部端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!