12

我已经开始尝试使用 Backbone.js,并且对 Backbone.Model 上的url属性的文档感到震惊。

特别是,我正在构建一个 REST API,它使用 HATEOAS/超媒体来驱动客户端。

我可以看到 Backbone 为集合中的项目构建 URL 本身的默认行为的有用性,但就我而言,我更愿意使用已解析的数据构建模型 URL。

有没有人在 Backbone 上扩展/构建来做到这一点?也许建立在像HAL这样的“标准”之上?

编辑:

为了澄清,假设我有以下内容:

获取/订单>>

[
  {
     "_links": {
       "self": "/orders/123"
     }
     "name": "Order #123",
     "date": "2012/02/23"
  },
  {
     "_links": {
       "self": "/orders/6666"
     }
     "name": "Order #666",
     "date": "2012/03/01"
  },
]

我有一个 Order 模型,例如:

var Order = Backbone.Model.extend({
});

我希望该url属性自动从 HAL 中的“自我”引用中提取出来。我认为创建一个新的基本模型类似于(未测试):

var HalModel = Backbone.Model.extend({
  url: function() {
    return get("_links").self;
  },
});

想法?

4

4 回答 4

10

我已经扩展骨干来做到这一点,该库可在此处获得:

https://github.com/mikekelly/backbone.hal

于 2012-05-30T21:56:24.197 回答
4

感谢@Pete 的澄清。

我想我看到了你的提议,我想它可能会奏效。但是,在您的示例中,您首先必须知道/Ordersurl 才能获得订单。而且,如果您对 json 进行了重新设计以拥有一个id属性,那么您将非常接近骨干网的默认实现。

现在,如果您只想使用通用模型或基本模型(例如HALModel)并使用数据引导它,那么您的方法可能很有用并且肯定可以工作。但是,我会查看覆盖解析以将 url 拉出并将其设置在模型上:

parse: function(response) {
    this.url = response._links.self;
    delete response._links;
    return response;
}
于 2012-03-02T02:54:36.203 回答
2

我在此补充 Simon 的回应,以解释如何使用gomoob/backbone.hateoas轻松完成。

// Instanciation of an Hal.Model object is done the same way as you're 
// used to with a standard Backbone model
var user = new Hal.Model({
    firstName: "John",
    lastName: "Doe",
    _links: {
        avatar: {
            href: "http://localhost/api/users/1/avatar.png" 
        },
        self: {
            href: "http://localhost/api/users/1"
        }
    },
    _embedded: {
        address: {
            "city" : "Paris",
            "country" : "France",
            "street" : "142 Rue de Rivoli",
            "zip" : "75001",
            "_links" : {
                "self" : {
                    "href" : "http://localhost/api/addresses/1"
                }
            }
        }
    }
});

// Now we you can easily get links, those lines are equivalent
var link1 = user.getLink('avatar');
var link2 = user.getLinks().get('avatar'); 

// So getting self link is simple too
var self = user.getLink('self');

// All the Hal.Link objects returned by backbone.hateoas are in fact 
// standard Backbone models so its standard Backbone
link1.get('href');
link1.getHref();

// You can do more with shortcut methods if your HAL links 
// have more properties
link1.get('deprecation');
link1.getDeprecation();
link1.get('name');
link1.getName();
link1.get('hreflang');
link1.getHreflang();
link1.get('profile');
link1.getProfile();
link1.get('title');
link1.getTitle();
link1.get('type');
link1.getType();
linke1.get('templated');
link1.isTemplated();

// You can also manipulate embedded resources if you need
user.getEmbedded('address').get('city');
user.getEmbedded('address').getLink('self');
...

最后,我们提供了一个 Hal.Model.url() 实现,它比标准 Backbone url() 更强大,如果您使用 HAL,它非常有用。

// By default url() returns the href of the self link if this self 
// link is present
user.url(); 

// If the self link is not defined then url() has the same behavior 
// as standard Backbone url() method
// My user is link to a user collection having a URL equal to 
// 'http://localhost/user1'
user.url(); // http://localhost/users/1

// My user is not link to a user collection in this case the URL is 
// generate using the model urlRoot property by default
user.urlRoot = 'http://myserver/users';
user.url(); // http://localhost/users/1

// backbone.hateoas also allows you to define an application wide root 
// URL which prevent to use absolute URLs everywhere in your code
Hal.urlRoot = 'http://localhost/api';  // HAL root API URL

var user = new Hal.Model({ id : 1});
user.urlMiddle = 'users'; 
user.url(); // http://localhost/api/users/1

希望这会有所帮助,如果您需要这方面的帮助,请不要犹豫在我们的 github 上发布问题。

于 2015-07-02T05:36:28.957 回答
1

你可以覆盖模型上的 url 函数来计算你想要的 URL;它是完全可扩展的。

于 2012-03-01T17:41:28.603 回答