原型到JQuery转换 - 停留与class.create [英] Prototype to JQuery conversion - stuck with class.create

查看:217
本文介绍了原型到JQuery转换 - 停留与class.create的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新手,试图将代码从原型转换为JQuery。我通过的基础知识,但被困在下面的代码。我试过使用jquery extent但不能工作它。这里是代码,我真的很感谢一些帮助。

I'm a novice who's trying to convert code from prototype to JQuery. I'm getting through the basics but am stuck with the code below. I've tried using jquery extent but cannot work it out. Here is the code, I'd really appreciate some help.

var SaveForm = Class.create();
SaveForm.prototype = {
    formId: null,
    responseText: null,

    initialize: function(formId)
    {
            this.formId = formId;
    }, 
    sendRequest: function(idform)
    {
            var referenceThis = this;
            $(idform).request(
            {
                      onSuccess: function(transport)
                      {
                              referenceThis.responseText = transport.responseText;
                              referenceThis.onSuccess();
                      },
                      onFailure: function(transport)
                      {
                              referenceThis.responseText = transport.responseText;
                              referenceThis.onFailure();
                      }
            });
    },
    onSuccess: function()
    {
    },
    onFailure: function()
    {
    }
}


推荐答案

jQuery是为DOM,事件处理,Ajax动画。

jQuery is made for DOM, Event handling, Ajax and Animation. Creating "classes" is not in its scope.

但是,你可以很容易地将它转换为一个简单的纯JavaScript构造函数和原型方法:

However, you can easily transform it to a simple plain JavaScript constructor function and prototype methods:

function SaveForm(formId) {
    this.formId = formId;
    this.responseText = null;
}
SaveForm.prototype.sendRequest = function(idform){
    var _this = this;
    $(idform).request({
        onSuccess: function(transport){
            _this.responseText = transport.responseText;
            _this.onSuccess();
        },
        onFailure: function(transport){
            _this.responseText = transport.responseText;
            _this.onFailure();
        }
    });
};
SaveForm.prototype.onSuccess = function(){};
SaveForm.prototype.onFailure = function(){};

但是,我不知道这些方法中哪一个真正需要。 jQuery没有请求方法,并且做Ajax请求只使用强大的 $。ajax 函数,以及Defereds功能。听起来你需要的是

However, I'm not sure which of these methods you actually need. jQuery has no request method, and to do Ajax requests just use the powerful $.ajax function, and the Defereds functionality. Sounds like all you need is

 function submitForm(id) {
     var $form = $('#'+id);
     return $.ajax({
         url: $form.attr("target"),
         method: $form.attr("method"),
         data: $form.serialize(),
         dataType: "html"
     });
 }
 // Usage:
 submitForm("myForm")
   .done(function onSuccess(responseText) {…})
   .fail(function onFailure(error) {…});

这篇关于原型到JQuery转换 - 停留与class.create的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆