model.example.js | |
---|---|
Class hive.Model | var hive = require('hive'); |
Sample | exports = module.exports = hive.Model.extend({
_name: 'User', |
Behavior MethodsThis example shows how to override methods that are called by hive when you want to add functionality. .initialize(params, options)params [optional] An object containing the attributes the | |
options [optional] An object containing the attributes the | |
| initialize: function(params, options) {
console.log('A user has been created, but not saved.',
'It was created with these attributes',
params,
'...and these options...',
options);
},
|
.sync(method, model)method One of | |
model The model that is being synced. | |
If a model does not have a sync method of its own, hive will call the global | sync: function (method, model) {
console.log('A user was synced', method, model); |
By default hive will sync all models to a MongoDB. More on this here. | hive.sync(model, model);
},
|
.validate(params)params [optional] An object containing any parameters that are being set. | |
| validate: function() { |
hive's version of | var _self = this,
assert = this.assert; |
.describe(rules)rules An object containing keys that associate the validating function to a property of the model. | |
| return this.describe({
'email': {
'you must enter a valid email address': function(email) {
assert.email(email);
}
},
'password': {
'you must enter a password': function(password) {
if(_self.isNew()) {
assert.exists(password);
}
},
'a password must be atleast 4 characters': function(password) {
if(password) {
assert.true(password.length >= 4);
}
}
},
'name': {
'a name is required': function(name) {
assert.exists(name);
},
'a name can only be 256 chars in length': function(name) {
assert.true(name.length < 256);
}
}
});
},
|
DefaultsAll classes in hive provide a way to set default values. Simply set these values in the definition
of your class, then after your | defaults: {
'role': 'user'
},
|
EventsAll models in hive inherit from node's Event Emmitter class. | saw: {
'facebook:connected': function (e) {
var connections = this.get('connections') || {};
if(e.token) {
connections.facebook = e.token;
this.emit('connected:facebook');
} else {
this.error('connections', 'Could not connect to Facebook');
}
}
}
}); |
InstantiatingHive | |
This user is being created with an object of params, and an object of options. | var user = new hive.models.User({
email: 'hey@there.com',
name: 'Test Name'
},
{
silent: true
}); |
At this point we will have one error, since a password was not included. { password: 'you must enter a password' } | console.log(user.errors); |
Utility MethodsThese methods reduce repetitive code and help simplify models. | |
.set(params)params An object containing any parameters to be set. | |
| user.set({password: '12345'}); |
.save(params)params An object containing any new parameters to be set, if valid, before saved.
| user.save(); |
.fetch()
| user.fetch(); |
.destroy()
| user.destroy(); |
.ready(callback)callback is a function called one time after a model is done syncing, or when a model is idle. | user.ready(function () {
console.log('Done syncing...');
});
|