Giter Club home page Giter Club logo

angular-halresource's Introduction

angular-halresource โ€” HAL client for AngularJS applications

Travis Coveralls Bower License

A HAL client for AngularJS applications.

Usage documentation of the module is currently scarce, but the source is documented and tested and should be easy to follow. Improving the docs is on the roadmap.

For more information on HAL, see here for an introduction or the read the formal specification.

Example usage

Getting resources

var context, user;

context = new HalContext();
user = context.get('http://example.com/john');

user.$get().then(function () {
  console.log(user.name);
});

Putting resources

var context, user;

context = new HalContext();
user = context.get('http://example.com/john');

user.$get().then(function () {
  user.name = 'Jane';
  return user.$putState();
}).then(function () {
  console.log(user.name);
});

Note that this example uses the common idiom of putting the resource state as application/json instead of the full HAL representation including links.

Following relations

var context, user, car;

context = new HalContext();
user = context.get('http://example.com/john');

user.$get().then(function () {
  car = user.$rel('car');
  return car.$get();
}).then(function () {
  console.log(user.name);
  console.log(car.brand);
});

Loading resources

By using $load instead of $get a GET request will only be issued if the resource was not already synchronized with the server. This is useful for avoiding unnecessary GET requests for embedded resources.

In this example, if the user resource embeds the car resource, that resource will be extracted and added to the context. No GET request will be issued to load the car resource.

var context, user, car;

context = new HalContext();
user = context.get('http://example.com/john');

user.$load().then(function () {
  car = user.$rel('car');
  return car.$load();
}).then(function () {
  console.log(user.name);
  console.log(car.brand);
});

Applying profiles

var context, user;

HalContext.registerProfile('http://example.com/profiles/user', {
  fullName: {get: function () {
    return this.firstName + ' ' + this.lastName;
  })
});

context = new HalContext();
user = context.get('http://example.com/john');
user.$profile = 'http://example.com/profiles/user';

user.firstName = 'John';
user.lastName = 'Snow';
console.log(user.fullName);

If the representation received from a GET request contains a profile link, it is applied automatically.

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.