BackboneJS:哪里声明视图中的作用? [英] BackboneJS : where to declare the function inside a view?
问题描述
我开始学习BackboneJS。这里是我的code:
I'm starting to learn BackboneJS. Here is my code :
var TodoItem = Backbone.Model.extend({});
var todoItem = new TodoItem({
description: 'Pick up milk',
status: 'incomplete',
id: 1
});
var TodoView = Backbone.View.extend({
render: function()
{
var html = '<h3>' + this.model.get('description') + '</h3>';
$(this.el).html(html);
}
});
var todoView = new TodoView({ model: todoItem });
todoView.render();
console.log(todoView.el);
但我得到这个错误:
But I get this error :
Uncaught TypeError: Expecting a function in instanceof check, but got undefined
_.extend._setElement @ backbone.js:1233
_.extend.setElement @ backbone.js:1222
_.extend._ensureElement @ backbone.js:1302
Backbone.View @ backbone.js:1170
child @ backbone.js:1831
(anonymous function) @ (index):28
我在做什么错?我真的不明白的地方,我需要创建它的等待功能。
What am I doing wrongly ? I don't really understand where I need to create the function it's waiting for.
推荐答案
在Backbone.js的, _setElement
用来设置这一点。$埃尔
和 this.el
。您的特定错误是在第一行发生在以下Backbone.js的code:
In backbone.js, _setElement
is used to set the this.$el
and this.el
. Your particular error is happening on the first line in the following Backbone.js code:
_setElement: function(el) {
this.$el = el instanceof Backbone.$ ? el : Backbone.$(el);
this.el = this.$el[0];
},
正如你所看到的,我们正在检查,如果它是一个instanceof骨干$,但基于您的错误骨干。$为空。 这个错误提示的jQuery既没有加载或没有在页面上。请确保您包括jQuery的,你包括你的页面上骨干了。
下面是所需的例子需要使用一些的CDN承载这些库。
Here's an example of the needed requires using some CDNs that host these libraries.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.1/backbone-min.js"></script>
这篇关于BackboneJS:哪里声明视图中的作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!