Giter Site home page Giter Site logo

liddellj / backbone.hypermedia Goto Github PK

View Code? Open in Web Editor NEW

This project forked from linn/backbone.hypermedia

0.0 1.0 0.0 60 KB

Backbone plugin providing support for following hypermedia controls from Backbone models.

License: MIT License

JavaScript 100.00%

backbone.hypermedia's Introduction

Build Status Bower Version npm Version Nuget Version backbone.hypermedia

Backbone plugin providing support for following hypermedia controls from Backbone models and collections.

Basic Usage

Assuming a resource with a links property, something like this:

{
	"name": {
		"first" : "Joe",
		"last": "Bloggs"
    },
    "links" : [
    	{ "rel": "country", "href": "/countries/GB" }
    ]
}

Define a corresponding Backbone model by extending Backbone.HypermediaModel (instead of Backbone.Model):

var User = Backbone.HypermediaModel.extend({
   ...
});

Then define a links property for the model like this:

links: {
	'country': Country,
	'timezone': TimeZone
}

Each key corresponds to the rel value of a link which may be present when the model is fetched from the server. The value defines the type of Backbone model you would like to be constructed based on that relation, so in this example Country and TimeZone are Backbone models (which must themselves extend Backbone.HypermediaModel).

Backbone.HypermediaModel overrides fetch so that for each link found in the response, if there is a corresponding value in the links property on your model, then the related model will also be fetched and a property added to your model under the same key. The return value of fetch is a promise which represents the collective fetch operations for the model and all related resources.

By using the promise returned from fetch you can wait until all related resources are fetched before rendering your view:

user.fetch().then(function () {
	// show user view
});

You can also add a links property to each related resource and have those relations followed as well, if you need to.

toJSON will include any related models which have been added to your model as properties.

Installation

bower install backbone-hypermedia
npm install backbone-hypermedia
Install-Package backbone.hypermedia

Publishing

Prior to publishing a new version of the package, you must run the following commands to configure your NuGet and npm credentials. You should only need to do this once.

npm adduser
grunt nugetkey --key=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX

Once you have entered your credentials, you will have to get yourself added as a maintainer to both nuget and npm. Then you can publish to npm and NuGet by running one of the following tasks:

grunt publish:patch

Increments patch version in package.json, publishes to npm and NuGet. A short-hand command is grunt publish.

grunt publish:minor

As before, but bumps minor version.

grunt publish:major

As before, but bumps major version.

The publish task will create an appropriate semver tag which Bower will detect as a new version.

Contributors

backbone.hypermedia's People

Contributors

liddellj avatar mr-sandy avatar richardiphillips avatar wilhen01 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.