Just create your own validateAsync... hint: look at how Backbone._validate 
is implemented.

On Tuesday, March 20, 2012 7:12:57 AM 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