本文介绍了设置骨干指向外部端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在骨干网中,如何将我的实体指向外部端点?

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

推荐答案

在您的示例中,您为模型设置了海关urlRooturl属性.

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.

这篇关于设置骨干指向外部端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 01:44