Skip to content

Defining Attributes

Brett Cassette edited this page Aug 17, 2014 · 12 revisions

Attributes are easy to define on ngActiveResource models:

function TShirt(data) {
  this.string('brand');
  this.number('price');
  this.boolean('onSale');
  this.integer('timesResold');

  this.computedProperty('salePrice', function() {
    return this.price - this.price * 0.2;
  }, ['price']);
};

Attributes will automatically attempt to cast data received to the appropriate type, and they'll define validations for you.

Numbers

Numbers are allowed to be integers or floats. Otherwise, tshirt.$valid is false. As with all data defined on your models, the attribute is allowed to be undefined or null.

Integers

A bit stricter than number-validation: floats are not allowed.

Booleans

True or false values only. The validator also considers the string values "true" and "false" to be valid.

Strings

Strings contain no special validation logic. We just think the syntax is nicer than getting a hash of inputs, and saying:

function Point(attributes) {
  x = attributes.x;
  y = attributes.y;
}

Computed Properties add a whole new level to Angular development. Check them out next!