Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
166
rated 0 times [  173] [ 7]  / answers: 1 / hits: 16878  / 12 Years ago, wed, april 18, 2012, 12:00:00

When you override backbone sync, both model/collection .save()/fetch() uses the same backbone sync method, so what is the best way to check if what Backbone.sync recieves is a model or a collection of models?



As an example:



Backbone.sync = function(method, model, options){
//Model here can be both a collection or a single model so
if(model.isModel()) // there is no isModel or isCollection method
}


I suppose I am looking for a safe best practice, I could of course check for certain attributes or methods that only a model or a collection have, but it seems hackish, shouldn't there be a better obvious way? And there probably is I just couldn't find it.



Thanks!


More From » backbone.js

 Answers
10

You could also try instanceof like so:



Backbone.sync = function(method, model, options) {
if (model instanceof Backbone.Model) {
...
} else if (model instanceof Backbone.Collection) {
...
}
}

[#86174] Tuesday, April 17, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucianom

Total Points: 601
Total Questions: 98
Total Answers: 109

Location: Kenya
Member since Fri, Dec 23, 2022
1 Year ago
lucianom questions
Tue, Feb 22, 22, 00:00, 2 Years ago
Wed, May 5, 21, 00:00, 3 Years ago
Sun, Jan 24, 21, 00:00, 3 Years ago
Sat, Aug 15, 20, 00:00, 4 Years ago
Mon, Jun 22, 20, 00:00, 4 Years ago
;