Nodejs:期望检查instanceof中的函数但是未定义

时间:2015-05-21 16:02:17

标签: javascript node.js node-webkit

使用node-webkit时出现这个奇怪的错误,这是一个可以重新创建它的完整示例:

index.js:

var jQuery = require('jquery');
var Backbone = require('backbone');

(function($){
  var ListView = Backbone.View.extend({
    el: $('body'),
    initialize: function(){
      _.bindAll(this, 'render');    
       this.render();
    },
    render: function(){
      $(this.el).append("<ul> <li>hello world</li> </ul>");
    }
  });
  var listView = new ListView();
})(jQuery);

的index.html:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="./css/main.css" />
    </head>
    <body>
    </body>
 <script src="./js/index.js"></script>
</html>

堆栈跟踪:

TypeError: Expecting a function in instanceof check, but got undefined
at _.extend.setElement     (C:\SVN\mapthing\branches\mapthing_js\node_modules\backbone\backbone.js:1046:45)
at _.extend._ensureElement (C:\SVN\mapthing\branches\mapthing_js\node_modules\backbone\backbone.js:1108:14)
at Backbone.View (C:\SVN\mapthing\branches\mapthing_js\node_modules\backbone\backbone.js:1000:10)
at new child (C:\SVN\mapthing\branches\mapthing_js\node_modules\backbone\backbone.js:1566:41)
at file:///C:/SVN/mapthing/branches/mapthing_js/js/index.js:18:18
at file:///C:/SVN/mapthing/branches/mapthing_js/js/index.js:19:3

无法查看我的问题所在?

大多数解决方案似乎都指向首先需要jquery,但我已经这样做了......

我是一个亲戚n00b所以我期待我做了一件非常愚蠢的事......

1 个答案:

答案 0 :(得分:0)

这是一个hack,所以请注意,但是错误来自于骨干lib中的这个方法(来自堆栈跟踪的node_modules/backbone/backbone.js:1046):

setElement: function(element, delegate) {
  if (this.$el) this.undelegateEvents();
  this.$el = element instanceof Backbone.$ ? element : Backbone.$(element);
  this.el = this.$el[0];
  if (delegate !== false) this.delegateEvents();
  return this;
},

您最有可能通过在Backbone上显式设置jQuery来规避此错误,如下所示:

var jQuery = require('jquery');
var Backbone = require('backbone');
Backbone.$ = jQuery;

这是一个完全未经测试的猜测,但它应该有用。