-
Notifications
You must be signed in to change notification settings - Fork 8
Helpers
Alexey Gordeyev edited this page May 18, 2015
·
4 revisions
file location in project /app/helpers/ApplicationHelper.js
module.exports = {
getClientIp: function(req) {
var ipAddress;
// The request may be forwarded from local web server.
var forwardedIpsStr = req.header( 'x-forwarded-for' );
if( forwardedIpsStr ) {
// 'x-forwarded-for' header may return multiple IP addresses in
// the format: "client IP, proxy 1 IP, proxy 2 IP" so take the
// the first one
var forwardedIps = forwardedIpsStr.split( ',' );
ipAddress = forwardedIps[0];
}
if( !ipAddress ) {
// If request was not forwarded
ipAddress = req.connection.remoteAddress;
}
return ipAddress;
}
...
};
file location in project /app/helpers/ViewsHelper.js
module.exports = {
normalizeDate: function(date) {
if( typeof date === 'number' ) {
return new Date( date ).toISOString();
} else if( date.getTime ) {
return date.toISOString();
} else {
return new Date().toISOString();
}
}
...
};
Available Views Helper Methods here
file location in project /app/helpers/ModelsHelper.js
User.validatesPresenceOf('name', 'email')
User.validatesLengthOf('password', {min: 5, message: {min: 'Password is too short'}});
User.validatesInclusionOf('gender', {in: ['male', 'female']});
User.validatesExclusionOf('domain', {in: ['www', 'billing', 'admin']});
User.validatesNumericalityOf('age', {int: true});
User.validatesUniquenessOf('email', {message: 'email is not unique'});
user.isValid(function (valid) {
if (!valid) {
user.errors // hash of errors {attr: [errmessage, errmessage, ...], attr: ...}
}
})
User.hasMany(Post, {as: 'posts', foreignKey: 'userId'});
// creates instance methods:
// user.posts(conds)
// user.posts.build(data) // like new Post({userId: user.id});
// user.posts.create(data) // build and save
Post.belongsTo(User, {as: 'author', foreignKey: 'userId'});
// creates instance methods:
// post.author(callback) -- getter when called with function
// post.author() -- sync getter when called without params
// post.author(user) -- setter when called with object
// work with models:
var user = new User;
user.save(function (err) {
var post = user.posts.build({title: 'Hello world'});
post.save(console.log);
});
User.prototype.getNameAndAge = function () {
return this.name + ', ' + this.age;
};
Post.scope('active', { published : true });
Post.active(function(err, posts){
// your code here
});
Models The following callbacks supported:
- afterInitialize
- beforeCreate
- afterCreate
- beforeSave
- afterSave
- beforeUpdate
- afterUpdate
- beforeDestroy
- afterDestroy
- beforeValidation
- afterValidation
User.afterUpdate = function (next) {
this.updated_ts = new Date();
this.save();
// Pass control to the next
next();
};