Skip to content

Commit

Permalink
added record modification example
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierlacot committed Jul 11, 2011
1 parent f89afb3 commit 70e3233
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
19 changes: 16 additions & 3 deletions Resources/app.js
Expand Up @@ -21,14 +21,19 @@ win.open();

joli.models.initialize();

// create a "City" record and persist it
// create some "City" records and persist them
var newYork = models.city.newRecord({
name: 'New York',
description: 'A big Apple'
});
newYork.save();
var paris = models.city.newRecord({
name: 'Paris',
description: 'Cheese and Baguette'
});
paris.save();

label.text = 'New York saved in database';
label.text = 'New York and Paris saved in database';

// create a "human" record (not persisted)
var john = models.human.newRecord({
Expand Down Expand Up @@ -56,4 +61,12 @@ label.text = label.text + '\nSarah created and saved in database';

// count the number of New York habitants
var count = models.human.countIn('New York');
label.text = label.text + '\nThere are ' + count + ' inhabitants in New York';
label.text = label.text + '\nThere are ' + count + ' inhabitants in New York';

var john = models.human.findOneById(1);
john.move('Paris');
label.text = label.text + '\nJohn moved to Paris';
john.save();

var john = models.human.findOneById(1);
label.text = label.text + '\nJohn now lives in: ' + john.getCityName();
10 changes: 10 additions & 0 deletions Resources/lib/model/models.js
Expand Up @@ -37,6 +37,16 @@ var models = (function() {
} else {
this.set('city_id', city.id);
}
},
getCityName: function() {
// search for the city id
var city = joli.models.get('city').findOneById(this.city_id);

if (!city) {
throw 'Could not find a city for this person!';
} else {
return city.name;
}
}
}
});
Expand Down

0 comments on commit 70e3233

Please sign in to comment.