简体   繁体   English

Backbone.js / jQuery表单提交

[英]Backbone.js / jQuery Form Submission

I seem to be having an issue with this little snippet. 我似乎对这个小片段有疑问。 I've fiddled with it for quite some time and it never truly seems to catch a Form Submission, no matter how I set my View Object. 我已经花了很长时间来摆弄它,无论我如何设置视图对象,它似乎从未真正收到过表单提交。 As I've come to understand Backbone, the View model is supposed to tie to the DOM through jQuery (although not always necessary), which is why I planned on catching the submit in this fashion to ensure simple serialization and ajax posting. 当我了解Backbone时,View模型应该通过jQuery绑定到DOM(尽管并非总是必要的),这就是为什么我计划以这种方式捕获提交以确保简单的序列化和Ajax发布。 Although, every time I submit I never actually see "Caught Submission!" 虽然,每次我提交时,我实际上都从未看到过“提交成功!” in the Console Log. 在控制台日志中。

Any help with this issue is greatly appreciated. 非常感谢您对此问题的任何帮助。

JSFiddle: http://jsfiddle.net/alexgurrola/3a2hrhL3/ JSFiddle: http : //jsfiddle.net/alexgurrola/3a2hrhL3/

/**
 * DOM Ready Handler
 * @type {{ready: Function, load: Function, unload: Function}}
 */
var DOM = {
    ready: function (fn) {
        (document.readyState !== 'loading') ? fn() : document.addEventListener('DOMContentLoaded', fn);
    },
    load: function (fn) {
        (document.readyState === 'complete') ? fn() : window.addEventListener('load', fn);
    },
    unload: function (fn) {
        window.addEventListener('beforeunload', fn);
    }
};

/**
 * Stratus Layer Prototype
 * @type {{Models: {}, Collections: {}, Views: {}, Routers: {}}}
 */
var Stratus = {
    Models: {},
    Collections: {},
    Views: {},
    Routers: {},
    Events: {}
};

/**
 * Stratus Core Events
 *
 * When these events are triggered, all functions attached to the event name
 * will execute in order of initial creation.  This becomes supremely useful
 * when adding to the Initialization and Exit Routines below as the script(s)
 * progressively add Models, Functions, et cetera.
 */
_.extend(Stratus.Events, Backbone.Events);

// Init Event
Stratus.Events.on('init', function () {
    console.log('Stratus Layer Initialization!');
});

// Exit Event
Stratus.Events.on('exit', function () {
    console.log('Stratus Layer Exit!');
});

/**
 * Stratus DOM Ready Events
 *
 * When these events are triggered, all arguments will pass to their inherent
 * functions, which should allow the internal functions to be overwritten if
 * need be.  The scope of these events intends to provide solid reusable
 * methods that only require triggering.
 */
DOM.ready(function () {

    // Trigger Init Event
    Stratus.Events.trigger('init');

    var Forms = new Stratus.Collections.Forms();
    new Stratus.Views.Form({ collection: Forms });

});

/**
 * Form Model
 */
Stratus.Models.Form = Backbone.Model.extend({
    url: '/echo/json',
    validate: function (fields) {
        var result = validator.validateAll(fields);
        if (result !== true) return result;
    }
});

/**
 * Form Collection
 */
Stratus.Collections.Forms = Backbone.Collection.extend({
    model: Stratus.Models.Form,
    url: '/echo/json'
});

/**
 * Form View
 */
Stratus.Views.Form = Backbone.View.extend({
    tagName:'form',
    el: '#formAsync',
    events: {
        'submit form': 'catchSubmit'
    },
    initialize: function() {
        console.log('Form Initialized');
    },
    catchSubmit: function(e) {
        console.log("Caught Submission!");
        e.preventDefault();
    }
});

Thanks to the comments above, I was able to get this working simply by changing the last portion of the code below to reflect the submit event for the View Object, rather than a submit event for a form tag within the View Object. 由于上述评论,我能得到这个工作只需改变下面的代码的最后部分,以反映对视图对象提交事件,而不是在视图对象中的窗体标签的提交事件。

/**
 * Form View
 */
Stratus.Views.Form = Backbone.View.extend({
    tagName:'form',
    el: '#formAsync',
    events: {
        'submit': 'catchSubmit'
    },
    initialize: function() {
        console.log('Form Initialized');
    },
    catchSubmit: function(e) {
        console.log("Caught Submission!");
        e.preventDefault();
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM