Customize model functions

124 views
Skip to first unread message

edugg

unread,
Apr 16, 2013, 9:48:50 AM4/16/13
to ged...@googlegroups.com
GeddyVersion: 0.7.21
NodeVersion: 8.22


Hi,

I have a very short question, How can I customize model functions?

I mean doing something like this:

geddy.model.WhateverModel.Myfunction(...).

Can we create "Myfunction" to return a set of models, given a customized query?

thank you!

Miguel Madero

unread,
Apr 16, 2013, 11:40:20 AM4/16/13
to ged...@googlegroups.com
You can add a static function. There's a commentin the code showing this:

// Can also define static methods and properties
WhateverModel.someStaticMethod = function () {
  // Do some other stuff
};


You could write a custom where clause and return the results. 



Miguel


--
The official community discussion group.
website: geddyjs.org, source: https://github.com/mde/geddy, group: https://groups.google.com/d/forum/geddyjs?hl=en
---
You received this message because you are subscribed to the Google Groups "GeddyJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geddyjs+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Matthew Eernisse

unread,
Apr 16, 2013, 2:28:31 PM4/16/13
to ged...@googlegroups.com
Probably good to keep in mind that anything retrieving stuff from the DB will be async, so you'll want to define your function to take the standard (err, data) style callback, and pass the results to that.

edugg

unread,
Apr 17, 2013, 5:58:40 AM4/17/13
to ged...@googlegroups.com
Thanks to both of you, 

Miguel, what I want is to have a global function, like geddy's ones (all, first, ...). Not sure if this is even possible, without getting into geddy source code...

My problem is that all my models have a domain attribute, so, always, I have to filter them according to the host. I could make a static function and copy-paste it to all of them, but I want something more global and scalable.

Miguel Madero

unread,
Apr 17, 2013, 5:00:42 PM4/17/13
to ged...@googlegroups.com
It's probably not the best way to do it, but it can work. 

* Create a custom function for register. Let's say, registerCustomModel or something that makes sense for what you're really doing. 
* That function will delegate to geddy.model.register which returns your Model constructor. 
* Create an object that contains all your custom functions
* Call utils.mixin to include those. 


// Somewhere.js
registerCustomModel = function registerCustomModel (name, ModelDefinition) {
  var ModelCtor = geddy.model.register(name, ModelDefinition);
  utils.mixin(ModelCtor, _createStaticMethodsMixin(name));
};

_createStaticMethodsMixin = function (name) {
  var obj = {};
  obj.customStaticMethod = function () {
    // You need the name to access tje model or the adapter
    var model = geddy.model[name]
      , adapter = geddy.model.adapters[name];
  };
};

Now in your models you can selectively call your custom register function. You could do something similar to add custom properties. If this is something you want to enforce for all of them, you could do something similar for each model after they have all been registered (not sure what's the best extension point for this, but I can check). To do this, get all the model names from the geddy.model.descriptionRegistry.

Let me know if you need some help, I'll have to check when is the best time to do it if you go with the second option. 



Miguel


edugg

unread,
Apr 18, 2013, 3:32:46 PM4/18/13
to ged...@googlegroups.com
ok, I'm such a newbie...

I have some questions:

1- What utils.mixin does? And How I include utils in Somewhere.js?

2- if I understand correctly, your idea is to replace geddy.model.register for registerCustomModel, right? For instance

suppose that I have a Todo model:

var custom = require("./custom");

var Todo = function(){

   
this.property('id','string');
   
this.property('domain','string');

}

Todo = custom.registerCustomModel('Todo',todo);

Then, in custom.js I write my "CustomAll" function:

_createStaticMethodsMixin = function (name) {
 
var obj = {};

  obj
.CustomAll = function (callback) {
   
// You need the name to access the model or the adapter

   
var model = geddy.model[name]
     
, adapter = geddy.model.adapters[name];

   
    model
.all({customquery},function(err,data){
   
if(err){callback(err,null); }
   
else{ callback(null,data);}
   
});
 
};
};


But, How I call "CustomAll" from the Todo Controller?

Thanks a lot, I really appreciate your help!


PD: I think I have sent you two mails... sorry for that...

edugg

unread,
Apr 19, 2013, 7:04:31 PM4/19/13
to ged...@googlegroups.com
I have been working on this and I solve most of my problems.
I'm replying myself in case anyone is interested or needs something similar.

Utils.mixin mix properties on an object to another one (Requires utilities).

Miguel, unfortonately, this is not working:

  utils.mixin(ModelCtor, _createStaticMethodsMixin(name));

because _createStaticMethodsMixin(name) is not an object, so what I have done is:

registerCustomModel = function registerCustomModel (name, ModelDefinition) {
 
var ModelCtor = geddy.model.register(name, ModelDefinition);


 
var obj = {};
  obj
.customStaticMethod = function () {

   
var model = geddy.model[name]
     
, adapter = geddy.model.adapters[name];

 
};
  utils
.mixin(ModelCtor, obj);
};



And when I want to call it from a Controller:

geddy.model.MODELNAME.customStaticMethod();


Now is working perfectly but I have another problem that I don't know how to solve it...

Imagine I want in the customStaticMethod do a model.get{target_model} in a generic way.

model = geddy.model[name] <---- Something like this but with a "get"

This is possible?

Again thank you very much, your solution is awesome :)

Miguel Madero

unread,
Apr 27, 2013, 11:32:26 PM4/27/13
to ged...@googlegroups.com
Hi, 

I'm sorry for the late response. I missed your email inside gmail's maze of labels and threads. 

I'm glad it's working. Now, about the last question, I'm not sure I understood correctly, but I think you already got the model that this static method belongs to (using `model = geddy.model[name]`). We can do this for two reasons, 1) we have a reference to the name (via the closure) 2) objects in JS are just like arrays that can be accesed using keys for their members. so object.myProperty is just a nicer syntax for object["myProperty"] or geddy.model.MyModel is the same as geddy.model["MyModel"]. 
If in your static method you want to access the get method for a given target, you have to know the name of the model and call it like this:
model["get"+childModelName](function () {} );

Also, if you wanted to get a bit fancier you could create other static methods dynamically so you don't have to pass a parameter to the method to give it the ChildModelName. You could query the modelRegistry to get those associations and create static methods with a name like doSomething{ChildModelName} instead of calling doSomething("MyChildModelName"). Now I'm not sure what the use case is, but I could help to make this a bit more concrete if what I'm saying doesn't make a lot of sense now. 



Miguel


matheus coppetti silveira

unread,
Sep 2, 2013, 6:05:49 AM9/2/13
to ged...@googlegroups.com
Miguel,

i have:

User.isAdmin=function(){
 
if (authType===('local account')) return 'LOCAL';
 
else return 'NOT';
}


.. but when i call it on my view:

<%= user.isAdmin %>

its not printin' 'LOCAL' or 'NOT', its printing all function code:

function (){ if (authType===('local account')) return 'LOCAL'; else return 'NOT'; }

Should I do something different?

Thanks

Ben Ng

unread,
Sep 2, 2013, 6:08:09 AM9/2/13
to ged...@googlegroups.com
Try with <%= yourfunction() %>? Without the parenthesis I believe it will print without executing.

Ben Ng
Reply all
Reply to author
Forward
0 new messages