Skip to content

Add rel and rels convenience functions #19

@liddellj

Description

@liddellj

Internally at Linn, we use convenience functions for retrieving the href value of a given relation by name.

Backbone.Model.prototype.rel = function (name) {
    if (!this.has('links')) {
        return null;
    }

    var link = $.grep(this.get('links'), function (l) { return l.rel == name; });
    if (link.length > 0) {
        return link[0].href;
    }

    return null;
};

This would be a useful addition to backbone.hypermedia, in addition to a rels function which would return an array of href values based on name:

Backbone.Model.prototype.rels = function (name) {
    if (!this.has('links')) {
        return [];
    }

    return $.grep(this.get('links'), function (l) { return l.rel === name; });
};

The implementations above are pulled from two separate code bases - in adding this to backbone.hypermedia, I'm sure we can implement the rel function by making use of the rels function to avoid duplication.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions