javascript 检查某个东西是否是主干js中的模型或集合

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10203982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:01:19  来源:igfitidea点击:

Check to see if something is a model or collection in backbone js

javascriptbackbone.js

提问by neph

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?

当您覆盖主干同步时,模型/集合 .save()/fetch() 使用相同的主干同步方法,那么检查 Backbone.sync 接收的是模型还是模型集合的最佳方法是什么?

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.

我想我正在寻找一种“安全”的最佳实践,我当然可以检查只有模型或集合具有的某些属性或方法,但这似乎很hackish,难道不应该有更好的明显方法吗?可能是我找不到它。

Thanks!

谢谢!

回答by fiskers7

You could also try instanceoflike so:

你也可以这样尝试instanceof

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

回答by Nicolas Zozol

@fiskers7's answer works with deep extension :

@fiskers7 的答案适用于深度扩展:

        var Item = Backbone.Model.extend({
            className : 'Item',
            size :10
        });

        var VerySmallItem = Item.extend({
            size :0.1
        });

        var item = new Item();
        var verySmall = new VerySmallItem();

        alert("item is Model ?" + (item instanceof Backbone.Model)); //true
        alert("verySmall is Model ?" + (verySmall instanceof Backbone.Model)); //true

回答by robrich

This is equally hackish, but a Backbone collection has a model property, and a model doesn't -- it is itself a model.

这同样是骇人听闻的,但是 Backbone 集合有一个模型属性,而模型没有——它本身就是一个模型。

Perhaps a safer method is model.toJSON() and see if the result is an object or an array. You're probably going to model.toJSON() in your custom Backbone.sync anyway, so though this is pretty computationally expensive, it would happen anyway.

也许更安全的方法是 model.toJSON() 并查看结果是对象还是数组。无论如何,您可能会在自定义 Backbone.sync 中使用 model.toJSON(),因此尽管这在计算上非常昂贵,但无论如何它都会发生。

回答by Exploit

You could do something like this.

你可以做这样的事情。

Backbone.Model.prototype.getType = function() {
    return "Model";
}

Backbone.Collection.prototype.getType = function() {
    return "Collection";
}

if(model.getType() == "Model") {}
if(model.getType() == "Collection"){}

回答by sarink

I'm not entirely sure how I feel about this because it seems a bit hackish, but I can't exactly think of why it would be super bad at the moment.

我不完全确定我对此的感受,因为它看起来有点骇人听闻,但我无法完全想到为什么它现在会非常糟糕。

Definitely simple, and faster than an "instanceof" check (I'm assuming you won't name any other functions "isBBModel/Collection" on your objects?)

绝对简单,并且比“instanceof”检查更快(我假设您不会在对象上命名任何其他函数“isBBModel/Collection”?)

Backbone.Model.prototype.isBBCollection = function() { return false; }
Backbone.Model.prototype.isBBModel = function() { return true; }
Backbone.Collection.prototype.isBBCollection = function() { return true; }
Backbone.Collection.prototype.isBBModel = function() { return false; }