【问题标题】:I cannot capture the click event when using jquery dialog inside backbone在主干中使用 jquery 对话框时,我无法捕获单击事件
【发布时间】:2015-05-02 04:02:49
【问题描述】:

我在主干应用程序中有一个 jQuery UI 对话框。我完全按照post 中的建议进行操作,但是在单击对话框中的“登录”按钮时,我仍然无法捕获单击事件。我在 firebug 中不断收到此错误(在 jquery.js 中):

TypeError: click is undefined
click.apply( that.element[ 0 ], arguments );

这是我的 index.html:

    <!doctype html>
<html class="no-js" lang="en"> 
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,initial-scale=1">
        <meta name="description" content="">

        <title>Giip</title>
        <link rel="stylesheet" href="css/jquery_ui.css">
        <link rel="stylesheet" href="css/jquery_ui_theme.css">
        <link rel="stylesheet" href="css/style.css">

        <script data-main="js/others/app" src="js/libs/requires/require.js"></script>
    </head>
    <body>
    </body>                                                                                                                                                                                                                                  
</html> 

这是我的 app.js:

//Setup app
require.config({
    paths: {
        jquery: "../libs/jqueries/jquery",
        underscore: "../libs/underscores/underscore",
        backbone: "../libs/backbones/backbone",
        jqueryui: "../libs/jquery_uis/jquery_ui",
        text: "../libs/requires/plugins/text",
        templates: "../../templates"
    },  
    urlArgs: "bust=" +  (new Date()).getTime()
});

//Kick off app
require(["../views/giip/login_view"], function(LoginView) {
    loginView = new LoginView();
    loginView.render();                                                                                                                                                                                                                      
});

这是我的 login_new.js:

define([
    "jquery",
    "underscore",
    "backbone",
    "jqueryui",
    "text!templates/giip/login.html" 
], function($, _, Backbone, jQueryUi, loginTemplate) {
    var LoginView = Backbone.View.extend({

        login: function() {
            console.log("logging in...");
        },  

        events: {
            "click .ui-button": "login"
        },  

        render: function () {
            $(loginTemplate).dialog({
                title:"Login",
                autoOpen: true, 
                closeOnEscape: false,
                resizable: false,
                draggable: true,
                dialogClass: "no-close",
                modal: true,
                buttons: [{ text: "Login" }]
            });                                                                                                                                                                                                                              
            this.el = $("#dialogContainer");
            this.delegateEvents(this.events);
            return this;
        },  

        initialize: function () {
            _.bindAll(this, "render");
        }   
    }); 

    return LoginView;
});

这是我的登录模板:

<div id="dialogContainer" title="Create new user">                                                                                                                                                                                           
    <form>
        <fieldset>
            <label for="name">Username</label>
            <input type="text" name="username" id="username" value="phamtranquocviet" class="text ui-widget-content ui-corner-all">
            <div class='clear-both vertical-spacer'/>
            <label for="password">Password</label>
            <input type="password" name="password" id="password" value="xxxxxxx" class="text ui-widget-content ui-corner-all"> 
        </fieldset>
    </form>
</div>

当我在“渲染”函数中添加一个点击事件时,点击事件被捕获,我可以在 firebug 中看到“button clicked..”调试消息。但这不会是主干方式。请帮我找出我做错了什么?谢谢。

render: function () {
            $(loginTemplate).dialog({
                title:"Login",
                autoOpen: true, 
                closeOnEscape: false,
                resizable: false,
                draggable: true,
                dialogClass: "no-close",
                modal: true,
                buttons: [{ text: "Login" }]
                click: function() {
                    console.log("button clicked...");                                                                                                                                                                                        
                }
            });
            this.el = $("#dialogContainer");
            this.delegateEvents(this.events);
            return this;
        },

【问题讨论】:

    标签: jquery-ui backbone-events


    【解决方案1】:

    问题是您没有为 jQuery UI 对话框按钮选项传递点击回调函数。为什么要让事情变得复杂,只是让事情变得更干净,就像

    login_template,注意

    <div id="dialogContainer">..</div>
    

    改为

    <script type="text/template" id="dialog_template">
    

    使用下划线模板,完整文件如下

    <script type="text/template" id="dialog_template">
        <form>
            <fieldset>
                <label for="name">Username</label>
                <input type="text" name="username" id="username" value="phamtranquocviet" class="text ui-widget-content ui-corner-all">
                <div class='clear-both vertical-spacer'/>
                <label for="password">Password</label>
                <input type="password" name="password" id="password" value="xxxxxxx"   class="text ui-widget-content ui-corner-all"> 
            </fieldset>
        </form>
    </script>
    

    login_new.js

    define([
    "jquery",
    "underscore",
    "backbone",
    "jqueryui",
    "text!templates/giip/login.html" 
    ], function($, _, Backbone, jQueryUi, loginTemplate) {
    var LoginView = Backbone.View.extend({
    
        login: function() {
            console.log("logging in...");
        },  
    
        render: function () {
           var template = _.template( $("#dialog_template").html(), {} );
           this.$el.html( template ).dialog({
                title:"Login",
                autoOpen: true, 
                closeOnEscape: false,
                resizable: false,
                draggable: true,
                dialogClass: "no-close",
                modal: true,
                buttons: { Login: this.login}
            });
            return this;
        },  
    
        initialize: function () {
            _.bindAll(this, "render");
        }   
    }); 
    
    return LoginView;
    

    });

    演示可以在这里找到http://jsfiddle.net/0vk2qg87/

    【讨论】:

    • 感谢您的回答。这样可行。但是,events: { "click .ui-button": "doLogin" } 没有被使用,因此严格来说不是处理事件的主干方式。我知道你在小提琴中的代码有“事件”,但即使你把它拿出来它也能工作。
    猜你喜欢
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-22
    相关资源
    最近更新 更多