当您覆盖骨干同步时,model/collection .save()/ fetch()使用相同的骨干同步方法,那么检查if Backbone.sync收到的是一个模型还是一组模型?检查是否某个模型或集合在骨干js
举个例子:
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
}
我想我要寻找一个“安全”的最佳实践,我可以为某些属性,或者只是一个模型或集合有方法当然检查,但似乎哈克,不应该有更好的明显方式吗?而且我可能无法找到它。
谢谢!
给予好评,因为我是在检查在一些快速编码中,'length'属性(仅限Collection ...?!?)!检查“模型”是比较明智的。哈哈。 – eightyfive 2014-11-26 01:42:21
你们不应该指望这样的东西,因为它们可能会在将来发生变化并破坏你的代码。按照描述使用instanceof关键字:) – 2015-02-16 09:41:41