npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

backbone-relational-hal

v0.1.3

Published

Use HAL+JSON with Backbone.js and Backbone-relational.js.

Downloads

13

Readme

backbone-relational-hal

Consume HAL+JSON APIs with Backbone.js and Backbone-relational.js.

NPM version Build Status

Requirements

backbone-relational-hal is currently tested with the following libraries:

Installation

With bower:

bower install --save backbone-relational-hal

Builds:

Usage

This library adds a new Backbone.RelationalHalResource class that you can extend like Backbone.RelationalModel or Backbone.Model.

var Person = Backbone.RelationalHalResource.extend({
  url: '/people/2'
});

HAL Links

HAL Embedded Resources

Hyperlinks

HAL resources can parse HAL+JSON links out of the box.

Let's instantiate the above sample class and fetch data from the server.

var person = new Person();
person.fetch();

Assume the server would return this data containing several links.

{
  "_links": {
    "self": { "href": '/people/2' },
    "search": { "href": '/people/2/search{?email}', "templated": true },
    "alternate": [
      { "href": '/people/2.html', "type": 'text/html' },
      { "href": '/people/2.xml', "type": 'application/xml' }
    ]
  },
  "name": "Nobody"
}

Use the link method of a HAL resource to retrieve useful link objects.

// get a link object
person.link('self'); // => a Backbone.RelationalModel

// get the href of a link
person.link('self').href(); // => '/people/2'

// get a list of links
var alternates = person.link('alternate', { all: true }); // => Backbone.Collection of link objects

// get one of the links in the list
alternates.at(0).href(); // => '/people/2.html'

// find a link matching criteria
// (using Backbone's Underscore proxy methods)
alternates.findWhere({ type: 'application/xml' }).href(); // => '/people/2.xml'

URI Templates

Templated links can be expanded using the uri-templates library.

// get a templated link without parameters
person.link('search').href(); // => '/people/2/search'

// expand a link template with parameters
person.link('search').href({ template: { email: '[email protected]' } }); // => '/people/2/[email protected]'

Generating <a> Tags from Links

The tag method of link objects can generate HTML link tags for you.

// simple tag with text content
person.link('self').tag('John'); // => $('<a href="/people/2">John</a>')

// HTML content
var content = $('<strong />').text('Jane');
person.link('self').tag(content, { html: true }); // => $('<a href="/people/2"><strong>Jane</strong></a>')

Embedding Resources

Define embedded resources with the halEmbedded property when extending a HAL resource. Embedded resources are Backbone-relational.js relations and use the same options.

var Company = Backbone.RelationalHalResource.extend({

  halEmbedded: [
    {
      type: Backbone.HasOne,
      key: 'http://example.com/rel/manager',
      relatedModel: Person
    },
    {
      type: Backbone.HasMany,
      key: 'http://example.com/rel/employees',
      relatedModel: Person
    }
  ]
});

You can also use an equivalent HAL-like syntax where the key in the halEmbedded object is automatically used as the relation key.

var Company = Backbone.RelationalHalResource.extend({

  halEmbedded: {
    'http://example.com/rel/manager': {
      type: Backbone.HasOne,
      relatedModel: Person
    },
    'http://example.com/rel/employees': {
      type: Backbone.HasMany,
      relatedModel: Person
    }
  }
});

Assume the server would return this data for a company.

{
  "name": "Initech",
  "_embedded": {
    "http://example.com/rel/manager": {
      "name": "Bill Lumbergh"
    },
    "http://example.com/rel/employees": [
      {
        "name": "Peter Gibbons"
      },
      {
        "name": "Michael Bolton"
      },
      {
        "name": "Samir Nagheenanajar"
      }
    ]
  }
}

You can retrieve embedded resources with the embedded method.

// get an embedded resource
company.embedded('http://example.com/rel/manager'); // => Person
company.embedded('http://example.com/rel/manager').get('name'); // => 'Bill Lumbergh'

// get a Backbone.Collection of embedded resources
var employees = company.embedded('http://example.com/rel/employees'); // => Backbone.Collection

// get one of the resources in the collection
employees.at(0); // => Person
employees.at(0).get('name'); // => 'Peter Gibbons'

Meta

  • Author: Simon Oulevay (Alpha Hydrae)
  • License: MIT (see LICENSE.txt)