在Backbone.js的应用程序设置一个全球性的REST根URL [英] Setting a global REST root url in a Backbone.js application

查看:105
本文介绍了在Backbone.js的应用程序设置一个全球性的REST根URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Backbone.js的,你必须手动设置每一个模型,则rootURL。有没有我们可以在一个地方一旦设置这样的方式,所有车型将使用它吗?

In backbone.js, you have to set the rooturl of every model manually. Is there a way we can set this in a single location once and all models will use it?

有关如。 api.site.com 将REST服务,但是出于测试目的,它可能是在本地主机:1000 我需要能够改变服务的根URL容易,而不是让他们遍布在存在于应用程序中的很多车型的地方。

For eg. api.site.com will be the REST service, but for testing purpose, it may be at localhost:1000 I need to be able to change the root url of the service easily and not have them all over the place in the many models that exist in the application.

推荐答案

不知道究竟你的意思是手动设置 urlRoot 为每个模型。
presumably您

Not sure what exactly you mean setting urlRoot for every model manually. Presumably you do

var MyModel = Backbone.Model.extend({

  urlRoot: '/mymodel',
  ...

});

吧?然后每个模型实例自然有着相同的 urlRoot 。任何发言权的其他模型实例 MyOtherModel 会有些不同的 urlRoot

right? Then each model instance naturally has the same urlRoot.Any other model instance of say MyOtherModel would have some different urlRoot.

如果由于某种原因,你需要使用相同的 urlRoot ,我猜想那将是因为车型份额的属性。这应该暗示继承或扩展,所以你可以这样做:

If for some reason you need to be using the same urlRoot, I would guess it would be because the models share attributes. This should hint to inheritance or extension so you could do:

var BaseModel = Backbone.Model.extend({
  urlRoot: '/mymodel'
});

var MyModel = BaseModel.extend({
  ...
});

这篇关于在Backbone.js的应用程序设置一个全球性的REST根URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆