My favorites | Sign in
Project Logo
                
Search
for
Updated May 01, 2007 by pelleb
Labels: Featured
QuickStart  
Getting started with dom-resource

DOM Resource Quick Start

You map an object to a resource by following standard rails conventions in naming your elements dom id's.

Example DOM

<div id="people" class="people">
	<div id="person_1" class="person">
		<div class="name">Pelle</div>
		<div class="email">pelleb@gmail.com</div>
	</div>
	<div id="person_2" class="person">
		<div class="name">Bob</div>
		<div class="email">bob@gmail.com</div>
	</div>
	<div id="person_3" class="person">
		<div class="name">I will delete you</div>
		<div class="email">delete@gmail.com</div>
	</div>
</div>

Resources

Resources are analogous to tables or classes. Each resource maps to a plural REST resource in RAILS speak.

Thus a DOM Resource with the id 'people' maps to '/people' on the server.

The $RES function

Here you can either work with the collection as a whole using the $RES(plural_id) function:

	$RES('people') => DOM element extended with special resource methods below
	$RES('people').count() => 3
	$RES('people').reload() => AJAX GET call to /people
	$RES('people').create({name:'Dwight',email:'dwight@dundermifflin.com'}) => AJAX POST call to /people
	$RES('people').find(2) => <div id="person_2"....
	$RES('people').all() => [array of all person elements]

These should all be pretty straight forward.

Records

Records represent individual objects on the server and are analogous to rows or objects.

The $REC function

You access an individual objects using $REC(singular_id) function:

	$REC('person_1') => DOM element extended with special resource methods below
	$REC('person_1').db_id() => 1
	$REC('person_1').get('name') => 'Pelle'
	$REC('person_1').resource() => $RES('people')
	$REC('person_1').destroy() => AJAX delete to /people/1 then removes from dom
	$REC('person_1').action('toggle') => AJAX post to /people/1;toggle then replaces element with result

Extending your Records

You can extend your objects yourself by creating a module definition:

	var Person={
		initialize:function(){
			this.name=this.getName();
			this.email=this.getEmail();
		},
		getName:function(){
			return this.get('name');
		},
		getEmail:function(){
			return this.get('email');
		}
	}

$REC() automatically deduces the class name (Singular Capitalized) and checks if it exists. If it does it extends the element with these methods and calls the initialize method without parameters.


Sign in to add a comment
Hosted by Google Code