Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rel and rels convenience functions #19

Open
liddellj opened this issue Oct 1, 2014 · 0 comments
Open

Add rel and rels convenience functions #19

liddellj opened this issue Oct 1, 2014 · 0 comments

Comments

@liddellj
Copy link
Contributor

liddellj commented Oct 1, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant