Closed paglias closed 10 years ago
You can just do:
var User = moko('User')
User.prototype.someJsonFormat = function() {
var json = this.toJSON();
delete json.encyptedPassword
delete json.somethingElse
return json
};
Does this work for you?
Also, you might find paramd helpful.
@rschmukler that code causes a maximum call stack size exceeded error because it ends up calling itself
Are you sure? This code sample works for me...
var moko = require('moko'),
co = require('co');
var User = moko('User');
User.attr('name').attr('password');
User.prototype.someJSON = function() {
var json = this.toJSON();
delete json.password;
return json;
};
co(function*() {
var user = yield new User({name: 'Bob', password: '12345' });
console.log(user.someJSON());
})();
oh yes that works I meant overriding the toJSON directly because it's used by Koa when printing json responses
2014-06-09 21:26 GMT+02:00, Ryan Schmukler notifications@github.com:
Are you sure? This code sample works for me...
var moko = require('./'), co = require('co'); var User = moko('User'); User.attr('name').attr('password'); User.prototype.someJSON = function() { var json = this.toJSON(); delete json.password; return json; }; co(function*() { var user = yield new User({name: 'Bob', password: '12345' }); console.log(user.someJSON()); })();
Reply to this email directly or view it on GitHub: https://github.com/MokoJs/moko/issues/7#issuecomment-45532904
Matteo Pagliazzi - paglias.net
Ah. Alright, I think this will do what you want then:
var moko = require('./'),
co = require('co');
var User = moko('User');
User.attr('name').attr('password');
var toJSON = User.prototype.toJSON;
User.prototype.toJSON = function() {
var json = toJSON.call(this);
delete json.password;
return json;
};
co(function*() {
var user = yield new User({name: 'Bob', password: '12345' });
console.log(user.toJSON());
})();
@rschmukler thanks you! i didn't thought about that
I would like not to expose some database fields publicly and would love the ability to specify an optional transform function to be executed after the normal toJSON output is created.
If you don't want to add this to the core library then calling
require('moko')(modelName)
could return a child class with a reference to thesuper
class allowing for custom methods to extend the original ones.