簡體   English   中英

骨干事件在Handlebars模板中不起作用

[英]Backbone event doesn't work in a Handlebars template

我是骨干新手,我不明白為什么這個事件不會觸發。 我嘗試了幾種不同的方法,但並非每種方法都有效。 屏幕正在渲染。 我在最新的cordova手機上進行了測試。

login.js

define(["jquery", "underscore", "kinvey", "handlebars",  "text", "text!templates/Login.html"],
function ($, _, Kinvey, Handlebars,  Text , source) {
 var Login = Backbone.View.extend({
    events: {
        "touchend #submit": "loggati",
        "touchend #signup": "goToSignup"
        },

    initialize: function () {
        console.log("init"); 

         /*  won't go this way either
            $('#submit').click(function(){
                alert('ciao');
            });
            $('#signup').click(function(){
                alert('ciao');
         */
            });
    },

    render: function (eventName) {
        console.log("rendering"); 
        var template = Handlebars.compile(source);
        var context = {color : "blue"};
        var html=template(context);
        $(document.body).html(html);

        return this;
    },

    goToSignup: function () {
        console.log("signup : fired"); 
        //stuff
    },

    loggati: function() {
        console.log("loggati : fired"); 
       //stuff
       }
});

return Login;

});

Login.html

 <div data-role="header" >
            <h1>Login</h1>
</div>
<div data-role="content">
                <div data-role="fieldcontain">
                    <input id= "username" placeholder="Username" value="" type="text">
                </div>
                <div data-role="fieldcontain">
                        <input id= "password" placeholder="Password" value="" type="password">
                </div>
                <a data-role="button" id="submit">Sign In</a>
                <a data-role="button"  id="signup">Register</a>
</div>

router.js

define(["jquery", "underscore", "kinvey", "views/Login", "views/GetToken", "views/Game",   "views/SignupPage", "views/Home", "views/Settings", "views/Rules"],
    function ($, _, Kinvey, Login, GetToken, Game, SignupPage, Home, Settings, Rules) {
    var Router= Backbone.Router.extend({
        routes: {
            "login": "login",
            "getToken": "getToken",
            "game": "game",
            "signupPage": "signupPage",
            "home": "home",
            "settings": "settings",
            "rules": "rules"
        },

        initialize: function () {
            console.log('initialize');
            this.currentView = undefined;
            var page;
            if( window.localStorage.getItem("username") && window.localStorage.getItem("username").length > 0)
            {       
                console.log('trovato utente loggato');
                document.getElementById("nomeSet").innerHTML = window.localStorage.getItem("username");
                document.getElementById("tokenSet").innersHTML = window.localStorage.getItem("token");
                page = new Home();
            }
            else 
                page = new Login();

            this.changePage(page);
            },

        login: function () {
                var page = new Login();
                this.changePage(page);
              },

        getToken: function () {
            var page = new GetToken({
            });
            this.changePage(page);
          },

        game: function () {
            var page = new Game({
            });
            this.changePage(page);
          },

        signupPage: function () {
            var page = new SignupPage({
            });
            this.changePage(page);
          },

        home: function () {
            var page = new Home({
            });
            this.changePage(page);
          },

        settings: function () {
            var page = new Settings({
            });
            this.changePage(page);
          },

        rules: function () {
            var page = new Rules({
            });
            this.changePage(page);
          },

        changePage: function (page) {
        console.log('changepage '+page);
            if(this.currentView) {
               this.currentView.remove();
               this.currentView.off();
                }
            this.currentView = page;
            page.render();

            }
    });

    return Router;
});

使用事件委托將主干視圖的事件附加到其el 每個視圖都有一個el ,如果您自己不指定,則您的視圖將使用一個空的<div>作為其el 您未在任何地方指定el ,因此您的視圖將為您創建<div>作為el render方法通常如下所示:

render: function() {
    var html = html_from_some_template;
    this.$el.html(html);
    return this;
}

但是你說的是:

$(document.body).html(html);

添加HTML。 你的觀點的el是不是body ,因此您的活動的工作。

一個快速的解決方法是添加:

el: 'body'

更改為視圖定義,然后將render更改為:

this.$el.html(html);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM