I believe there may be 3 possible solutions:

- You may try to use fibers to make Your async code looks like a sync. I 
gues it may work, but I'm not sure about it.
- Hack Backbone Model, it's sources are small and clear and it shouldn't be 
too complex.
- Create Your own async model. You can use any model implementation with 
Backbone.js, it's very modular and works well with custom stuff. 
For example, here's my own custom model I use with Backbone for my project 
(I did it because I don't like to write user.get('name') and prefer 
user.name and also for some other reasons)
https://github.com/alexeypetrushin/rad_core/blob/master/assets/rad/models.coffee
https://github.com/alexeypetrushin/rad_core/blob/master/assets/rad_spec/model_spec.coffee


On Tuesday, March 20, 2012 3:12:57 PM UTC+4, Bika wrote:
>
> hi,
>
> I'm trying to use backbone for "everything" on the server side too, and 
> got stuck with doing a proper validation. The problem is that backbone's 
> validate method return a boolean, and the validation mechanics should be 
> implemented inside of it, but with node these mechanics are asynchronous, 
> thus the validate method returns before evaluating my code.
>
> Here is a code sample that illustrates well my setup:
>
> Backbone.Model.extend({
>   validate: function(){
>     var result;
>     Jobs._withCollection(function(err, collection){
>       collection.count(query, function(count){
>          result = count > 1 ? "Job already exist" : null;
>       });
>     })
>     return result;
>   }
> })
>
> I might be able to improve this a little bit, but I'm not sure if this 
> would work:
>
> function _validate(){
>     Jobs._withCollection(function(err, collection){
>       collection.count(query, function(count){
>          yield count > 1 ? "Job already exist" : null;
>       });
>     })
> }
>
> Backbone.Model.extend({
>   validate: function(){
>     return _validate.call(this);
>   }
> })
>
> Especially not for a bit more complex situations where I use async for 
> example, and the return value is set in the last callback of async, like 
> here (this is obviously wrong as res is null at return):
>
> Backbone.Model.extend({
>   validate: function(){
>     var res;
>     async.parallel([
>         .. do stuff ..
>     ], function(err, results){
>         res = results;
>     })
>     return results;
>   }
> })
>
> Any ideas?
>
> Viktor
> close
>
>    
>

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to