Javascript 此项目位于主干视图上
我正在试图了解此主干视图中的Javascript 此项目位于主干视图上,javascript,backbone.js,Javascript,Backbone.js,我正在试图了解此主干视图中的此.item来自何处: Backbone.View.extend({ initialize: function() { var date = new Date(); this.date = date; this.month = date.getMonth(); this.day = date.getDay(); this.year = date.getFullYear();
此.item
来自何处:
Backbone.View.extend({
initialize: function() {
var date = new Date();
this.date = date;
this.month = date.getMonth();
this.day = date.getDay();
this.year = date.getFullYear();
this.calendar = new calendar();
_.bindAll(this);
},
showActivityItem: function(e) {
var target = $(e.currentTarget);
var id = target.attr("data-id");
var item = this.item;
}
});
项
似乎是一个主干模型,除了它是如何分配给项
属性的之外,我对此没有意见
当我在控制台中输出该项时,我得到:
child {cid: "c85", attributes: Object, _changing: false, _previousAttributes: Object, changed: Object…}
_changing: false
_events: Object
_pending: false
_previousAttributes: Object
attributes: Object
changed: Object
cid: "c85"
collection: child
__proto__: Surrogate
正在为每个事件调用showActivityItem:
events: {
'click #activityList .boxEntry': 'showActivityItem'
},
类中没有这样做的代码。在主干视图中,item
是否是某个主干模型的未记录访问器?它不在主干网站的查看属性列表中
谢谢。在您显示的代码之外的某个地方,您的代码(可能是无意中)向视图添加了一个
.item
属性
var model = new ItemModel(...);
// ...
var view = new ItemView({model: model});
// ...
view.item = model;
主干本身并没有定义一个名为
项
的属性,您可以通过搜索。由于您使用的是标准主干事件委派(除非有一些代码未包含在内),因此事件处理程序中的此
将设置为视图本身。这是有效的代码吗?没有类似item属性的东西,因此必须在初始化时分配模型。这只是代码的缩写版本。我已经添加了initialize方法。正如你所看到的,它没有被分配到那里,因此我对如何分配它感到困惑。未在视图中的任何位置指定它。我想它可能是在视图之外分配的。您能执行console.log(item)
吗?因为它应该是未定义的…您能告诉我们何时调用showActivityItem
吗?它看起来像是来自jQuery事件的回调。可能上下文(这)不是视图本身的上下文,而是jQuery传递的上下文。@Puigcerber我已将问题更新为包含console.log(item)
。