Backbone.js木偶项目视图单击事件不工作
我已经能够在我的木偶控制器中触发事件,如下所示:Backbone.js木偶项目视图单击事件不工作,backbone.js,marionette,backbone-events,Backbone.js,Marionette,Backbone Events,我已经能够在我的木偶控制器中触发事件,如下所示: ...snip... var Controller = {}; _.extend(Controller, Backbone.Events); // private var Layout = Marionette.Layout.extend({ template: _.template(layout), regions: { inspectorStart: "#inspector_start",
...snip...
var Controller = {};
_.extend(Controller, Backbone.Events);
// private
var Layout = Marionette.Layout.extend({
template: _.template(layout),
regions: {
inspectorStart: "#inspector_start",
loginStart: "#login_start",
playerStart: "#player_start"
}
});
// private
var _initializeLayout = function() {
console.log('initialize Start Layout...');
Controller.layout = new Layout();
Controller.layout.on('show', function() { **// This works**
Controller.trigger('startLayout:rendered'); **// This works**
});
vent.trigger('app:show', Controller.layout); **// This works**
};
// Listen for events ///////////////////////////////////////////
// controller attach a sub view
Controller.on("startLayout:rendered", function() { **// This works**
console.log('startLayout:rendered =>StartController'); **// This works**
// render views for the existing HTML in the template,...
var inspectorStartView = new InspectorStartView();
// and attach it to the layout (i.e. don't double render)
Controller.layout.inspectorStart.attachView(inspectorStartView);
Controller.trigger('inspectorStart:show', inspectorStartView); **// This works**
...snip...
然而,当我尝试使用扩展的木偶项目视图时,我无法使事件或触发器工作
// views/InspectorStartView.js
// -------
define(["jquery", "marionette", "text!templates/InspectorStart.html"],
function($, marionette, template){
var InspectorStartView = marionette.ItemView.extend({
template: template,
events: {
'click .starting_thumbnail' : 'start'
//'click #inspectorStart' : 'start' **// didn't work either**
},
//I had also tried triggers
triggers: {
//'click .starting_thumbnail' : 'inspectorStart:start'
'click #inspectorStart' : 'inspectorStart:start' **// didn't work either**
},
start:function (){ **// Doesn't get called**
console("Caught InspectorStartView click")
this.trigger("inspectorStart:start")
}
});
// Returns the View class
return InspectorStartView;
});
以下是模板:
<!-- InspectorStart.html -->
<div id="inspectorStart" class="thumbnail starting_thumbnail">
<img src="/img/detective-97715888.jpg" alt="Inspector" width="200px" height="300px">
<div class="caption">
<h3>Inspectors Enter Here</h3>
<p>Den Masters, Teachers, Parents, House Mothers, this is where you start.</p>
</div>
</div>
我需要做些别的事情来让div可以点击吗
在我的第一次尝试中,我传入App.vent并尝试使用它,但没有效果,现在我正在用{uu.extend(Controller,Backbone.Events)扩展控制器{}
根据@Ingro的一些调试建议,我记录了ItemView的$el id。这:
log([“InspectorStartView初始化”,this.$el])
这是输出。有nodeName:“DIV”,localName是DIV
["InspectorStartView initalise ", jQuery.fn.jQuery.init[1]]
0: "InspectorStartView initalise "
1: jQuery.fn.jQuery.init[1]
0: div
accessKey: ""
align: ""
attributes: NamedNodeMap
baseURI: "http://localhost:8001/index.html"
childElementCount: 1
childNodes: NodeList[3]
children: HTMLCollection[1]
classList: DOMTokenList
className: ""
clientHeight: 496
clientLeft: 0
clientTop: 0
clientWidth: 300
contentEditable: "inherit"
dataset: DOMStringMap
dir: ""
draggable: false
firstChild: #comment
firstElementChild: div#inspectorStart.thumbnail starting_thumbnail
hidden: false
id: ""
innerHTML: "<!-- InspectorStart.html -->↵<div id="inspectorStart" class="thumbnail starting_thumbnail">↵ <img src="/img/detective-97715888.jpg" alt="Inspector" width="200px" height="300px">↵ <div class="caption">↵ <h3>Inspectors Enter Here</h3>↵ <p>Den Masters, Teachers, Parents, House Mothers, this is where you start.</p>↵ </div>↵</div>"
innerText: "↵Inspectors Enter Here↵Den Masters, Teachers, Parents, House Mothers, this is where you start.↵↵"
isContentEditable: false
lang: ""
lastChild: div#inspectorStart.thumbnail starting_thumbnail
lastElementChild: div#inspectorStart.thumbnail starting_thumbnail
localName: "div"
namespaceURI: "http://www.w3.org/1999/xhtml"
nextElementSibling: null
nextSibling: null
nodeName: "DIV"
nodeType: 1
nodeValue: null
offsetHeight: 496
offsetLeft: 621
offsetParent: body
offsetTop: 388
offsetWidth: 300
<!-- all the on<eventname> were null -->
outerHTML: "<div><!-- InspectorStart.html -->↵<div id="inspectorStart" class="thumbnail starting_thumbnail">...snip..."
ownerDocument: #document
parentElement: li#inspector_start.span4
parentNode: li#inspector_start.span4
prefix: null
previousElementSibling: null
previousSibling: null
scrollHeight: 496
scrollLeft: 0
scrollTop: 0
scrollWidth: 300
spellcheck: true
style: CSSStyleDeclaration
tabIndex: -1
tagName: "DIV"
输出要小得多:
InspectorVartView instance this.$el :
<div>
<!-- InspectorStart.html -->
<div id="inspectorStart" class="thumbnail starting_thumbnail">…</div>
</div>
InspectorVartView实例此。$el:
…
@Ingro被包裹在一个div里,这有什么关系
有人看到我做错了什么吗
谢谢
Andrew(见下面的评论)发现我不知何故破坏了jquery。我使用JSFIDLE来证明我的代码是正确的。然后我开始考虑版本号要求,并替换jquery,它开始工作 感谢@Ingro的支持
Andrewdiv#inspectorStart是您视图的父元素,您的$el?在这种情况下,您应该在活动中尝试以下操作:
“单击”:“开始”
@Ingro感谢您的建议。我尝试了“单击”:“开始”它不起作用。我把名字从“开始”改为“火”,看看这个名字是否有区别。这没什么区别。看起来它应该是直截了当的,但由于某些原因它不是,这里是一个jsFiddle()的开始。我不知道如何添加主干网和木偶网,但这只是一个开始。您应该将其添加到左侧的“添加资源”面板中。无论如何,在JSFIDLE上复制您的结构并不容易,因为您还必须添加require、模板等。您是否尝试过在浏览器控制台上用jQuery手动绑定事件?类似于$(body).on(“click”,“.starting_缩略图”,function(){console.log(“YAY”);})
然后尝试单击?我们可以尝试以下操作:在视图中添加初始化
函数,并记录此操作。$el
。返回的元素是div#inspectorStart还是包装器?
/ controller show inspector in the layout / subview
Controller.on('inspectorStart:show', function(inspectorStartView) {
console.log('show inspector start view');
console.log(**'InspectorVartView instance this.$el : ', inspectorStartView.el**);
Controller.layout.inspectorStart.show(inspectorStartView);
});
InspectorVartView instance this.$el :
<div>
<!-- InspectorStart.html -->
<div id="inspectorStart" class="thumbnail starting_thumbnail">…</div>
</div>