Skip to content

Commit

Permalink
Merge pull request geddy#123 from OscarGodson/master
Browse files Browse the repository at this point in the history
Minor update to README to add colors to the code examples.
  • Loading branch information
techwraith committed Apr 16, 2012
2 parents da4c09b + b6173ba commit dbfe7e8
Showing 1 changed file with 101 additions and 88 deletions.
189 changes: 101 additions & 88 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,17 @@ Routes are similar to Merb or Rails routes.

**Basic routes**

router.match('/moving/pictures/:id').to(
{controller: 'Moving', action: 'pictures'});
```javascript
router.match('/moving/pictures/:id').to(
{controller: 'Moving', action: 'pictures'});

router.match('/farewells/:farewelltype/kings/:kingid').to(
{controller: 'Farewells', action: 'kings'});
router.match('/farewells/:farewelltype/kings/:kingid').to(
{controller: 'Farewells', action: 'kings'});

//Can also match specific HTTP methods only
router.match('/xandadu', 'get').to(
{controller: 'Xandadu', action: 'specialHandler'});
//Can also match specific HTTP methods only
router.match('/xandadu', 'get').to(
{controller: 'Xandadu', action: 'specialHandler'});
```

**Resource-based routes**

Expand Down Expand Up @@ -168,37 +170,38 @@ DELETE */snow_dogs/:id[.extension]<br/>
A simple controller that just responds with any
form-post/query-string params looks like this:

var SnowDogs = function () {
this.respondsWith = ['text', 'json', 'html'];
```javascript
var SnowDogs = function () {
this.respondsWith = ['text', 'json', 'html'];

this.index = function (params) {
this.respond({params: params});
};
this.index = function (params) {
this.respond({params: params});
};

this.add = function (params) {
this.respond({params: params});
};
this.add = function (params) {
this.respond({params: params});
};

this.create = function (params) {
this.respond({params: params});
};
this.create = function (params) {
this.respond({params: params});
};

this.show = function (params) {
this.respond({params: params});
};
this.show = function (params) {
this.respond({params: params});
};

this.update = function (params) {
this.respond({params: params});
};
this.update = function (params) {
this.respond({params: params});
};

this.remove = function (params) {
this.respond({params: params});
};
this.remove = function (params) {
this.respond({params: params});
};

};

exports.SnowDogs = SnowDogs;
};

exports.SnowDogs = SnowDogs;
```

## Content-negotiation

Expand All @@ -209,13 +212,15 @@ If you have a JSON-serializable JavaScript object you want to
return in JSON format, pass your JavaScript object to the
`respond` method in the action on that controller.

this.respondsWith = ['text', 'json'];
```javascript
this.respondsWith = ['text', 'json'];

this.show = function (params) {
// (Fetch some item by params.id)
item = {foo: 'FOO', bar: 1, baz: false};
this.respond(item);
};
this.show = function (params) {
// (Fetch some item by params.id)
item = {foo: 'FOO', bar: 1, baz: false};
this.respond(item);
};
```

## Models and validations

Expand All @@ -225,71 +230,79 @@ Ruby's ActiveRecord or DataMapper.

Here is an example of a model with some validations:

var User = function () {
this.property('login', 'string', {required: true});
this.property('password', 'string', {required: true});
this.property('lastName', 'string');
this.property('firstName', 'string');

this.validatesPresent('login');
this.validatesFormat('login', /[a-z]+/, {message: 'Subdivisions!'});
this.validatesLength('login', {min: 3});
this.validatesConfirmed('password', 'confirmPassword');
this.validatesWithFunction('password', function (s) {
// Something that returns true or false
return s.length > 0;
});

// Can define methods for instances like this
this.someMethod = function () {
// Do some stuff
};
};

// Can also define them on the prototype
User.prototype.someOtherMethod = function () {
// Do some other stuff
};

User = geddy.model.registerModel('User', User);
```javascript
var User = function () {
this.property('login', 'string', {required: true});
this.property('password', 'string', {required: true});
this.property('lastName', 'string');
this.property('firstName', 'string');

this.validatesPresent('login');
this.validatesFormat('login', /[a-z]+/, {message: 'Subdivisions!'});
this.validatesLength('login', {min: 3});
this.validatesConfirmed('password', 'confirmPassword');
this.validatesWithFunction('password', function (s) {
// Something that returns true or false
return s.length > 0;
});

// Can define methods for instances like this
this.someMethod = function () {
// Do some stuff
};
};

// Can also define them on the prototype
User.prototype.someOtherMethod = function () {
// Do some other stuff
};

User = geddy.model.registerModel('User', User);
```

Alternatively, you can use the `defineProperties` method to lay out your model:

var User = function () {
this.defineProperties({
login: {type: 'string', required: true}
, password: {type: 'string', required: true}
, lastName: {type: 'string'}
, firstName: {type: 'string'}
});
}
```javascript
var User = function () {
this.defineProperties({
login: {type: 'string', required: true}
, password: {type: 'string', required: true}
, lastName: {type: 'string'}
, firstName: {type: 'string'}
});
}
```

Creating an instance of one of these models is easy:

var params = {
login: 'alex'
, password: 'lerxst'
, lastName: 'Lifeson'
, firstName: 'Alex'
};
var user = User.create(params);
```javascript
var params = {
login: 'alex'
, password: 'lerxst'
, lastName: 'Lifeson'
, firstName: 'Alex'
};
var user = User.create(params);
```

Data-validation happens on the call to `create`, and any
validation errors show up inside an `errors` property on
the instance, keyed by field name. Instances have a `valid`
method that returns a Boolean indicating whether the instance
is valid.

// Leaving out the required password field
var params = {
login: 'alex'
};
var user = User.create(params);

// Prints 'false'
sys.puts(user.valid());
// Prints 'Field "password" is required'
sys.puts(user.errors.password);
```javascript
// Leaving out the required password field
var params = {
login: 'alex'
};
var user = User.create(params);

// Prints 'false'
sys.puts(user.valid());
// Prints 'Field "password" is required'
sys.puts(user.errors.password);
```

## Running the tests

Expand Down

0 comments on commit dbfe7e8

Please sign in to comment.