1

次のように、マリオネット コントローラーでイベントを発生させることができました。

 ...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...

ただし、拡張マリオネット ItemView を使用しようとすると、イベントやトリガーが機能しません。

// 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>

ポインターを取得できるように、starting_thumbnail クラスを作成する必要がありました。

.starting_thumbnail{
cursor: pointer;
}

div をクリック可能にするには、他に何かする必要がありますか?

最初の試みで App.vent を渡し、それを無駄に使用しようとしましたが、今は Controller {} を _.extend(Controller, Backbone.Events); で拡張しています。

@Ingro からのデバッグに関するアドバイスに従って、ItemView の this.$el ID をログに記録しました。これ: console.log(["InspectorStartView initalise ",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 -->↵&lt;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>↵&lt;/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 -->↵&lt;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"

もちろん、上記はinitializeメソッド内にあります。「新しい」ものを作成した後の違いを見たいと思いました:

/ 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">​…​&lt;/div>​
</div>​

divにラップされている@Ingroはなぜそれが重要なのですか?

誰かが私が間違っていることを見ていますか?

ありがとう、

Andrew jsFiddle (以下のコメントを参照)

4

1 に答える 1