DOM Resource Quick StartYou 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> ResourcesResources 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 functionHere 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. RecordsRecords represent individual objects on the server and are analogous to rows or objects. The $REC functionYou 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 resultExtending 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.
|