简体   繁体   English

为什么会出现错误:“ .isValid()”不是一个函数?

[英]Why do I get the error: “.isValid()” is not a function?

I am trying to validate a model called Book based on the availability of an attribute, with the help of this video . 我正在尝试借助此视频根据属性的可用性来验证一个名为Book的模型。 I am trying to follow the instructions on how to validate a Backbone.js model, but when I go to validate an instance of it called myBook using the .isValid() method, it returns the error: 我试图按照有关如何验证Backbone.js的模型的指令,但是当我去验证一个实例的叫myBook使用.isValid()方法,它返回的错误:

Uncaught TypeError: myBook.isValid() is not a function

Here is my code: 这是我的代码:

<script src="https://ajax.cdnjs.com/ajax/libs/backbone.js/0.3.3/backbone-min.js"></script>
<script src="https://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.4/underscore-min.js"></script>
<script language="Javascript">
  var Book = Backbone.Model.extend({
    validate: function(attrs) {
      if (!attrs.title) {
        return "What's a book without a title?!";
      }
    }
  });

  var myBook = new Book();
  /* validation is run in browser */
</script>

This was similar to what was in the video linked above. 这类似于上面链接的视频中的内容。 When I go into the console and run the .isValid() method, it returns said error. 当我进入控制台并运行.isValid()方法时,它返回上述错误。

It would have been hard to help you if you hadn't provided the code sample which has the answer in it. 如果您没有提供包含答案的代码示例,将很难为您提供帮助。

Backbone 0.3.3 doesn't have isValid yet. Backbone 0.3.3还没有isValid

It was added in Backbone 0.9.1 but got removed in 0.9.9 . 它是在Backbone 0.9.1添加的,但在0.9.9中被删除了。

It was improved and added back in 1.0 . 对其进行了改进,并在1.0中添加了它。

After upgrading Backbone and related to the comment about h.has is not a function , you also need to upgrade underscore.js as _.has was added in 1.3.1. 升级Backbone并与有关h.has is not a function的注释h.has is not a function ,您还需要升级underscore.js,因为在1.3.1中添加了_.has

You should really use the latest stable release of the libs for your project as you'll avoid incompatibilities like that. 您应该为项目真正使用最新的稳定发行版库 ,因为这样可以避免这种不兼容性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM