Created
August 9, 2013 16:00
-
-
Save lisp-ceo/6194801 to your computer and use it in GitHub Desktop.
Backbone.Model.fetch does not support the silencing of sync events. Further, ES5 lacks generator expressions, as such semaphore implementations require some hacks. Semaphores are useful in this context as populating a model with data from multiple sources is a model operation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Yolo = Backbone.Model.extend({ | |
sweetMethods : function... | |
... | |
sem : { | |
requiredSyncs : 2, | |
syncedSyncs : 0 | |
}, | |
fetch : function( options ){ | |
options = options ? _.clone(options) : {}; | |
if (options.parse === void 0) options.parse = true; | |
var model = this; | |
var success = options.success; | |
options.success = function(resp) { | |
if (!model.set(model.parse(resp, options), options)) return false; | |
if (success) success(model, resp, options); | |
model.sem.syncedSyncs += 1; | |
if( model.sem.syncSyncs >= model.sem.requiredSyncs ){ | |
this.trigger( 'sync', model, resp, options ); | |
} | |
}; | |
wrapError(this, options); | |
return this.sync('read', this, options); | |
} | |
} | |
}); | |
var yolo = new Yolo(); | |
yolo.on( 'sync', function(){ | |
console.log( 'yo I've been fetched twice naow.' ); | |
}); | |
yolo.fetch(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment