简体   繁体   English

在jQuery上排序几个函数

[英]Sequence several functions on jQuery

I have a function that stores several values from a HTML form, and that must work individually in order to store that info in any situation I need (ie before inserting on DB, or before udating info on DB...) 我有一个函数,该函数存储HTML表单中的多个值,并且必须单独工作才能在所需的任何情况下(例如,在插入DB之前,或在DB上查询信息之前)存储该信息。

I need to be able to tell the system to execute this function ( 'storeValues' ),and then execute any other (could be 'createNewClass' , 'updateExistingClass' ... whatever). 我需要能够告诉系统执行此功能( 'storeValues' ),然后执行其他任何功能(可以是'createNewClass''updateExistingClass' ……等等)。

How can I sequence this? 我该如何排序? I tried here to store values first and, WHEN DONE, execute another function aleting about a value, but it says "storeValues() is not defined", and it is defined: 我在这里尝试先存储值,然后在完成后执行另一个有关值的函数,但是它说“ storeValues()未定义”,并且已定义:

$('.tableClassHeader').on('click', '.createClass', function(){
    storeValues().promise().done(function(){
        createNewClass();
    });
});

function storeValues(){
    cl_year = $('.newClassForm').find('select[name=cl_year]').val();
    cl_course = $('.newClassForm').find('select[name=cl_course]').val();
    }

function createNewClass(){
    alert(cl_year);}

I mean that storeValues function SHOULD BE a separate function with the possibility of being called from any other place, I know this problem could be solved by executing "createNewClass" from the "storeValues" function, but there will be times that I need to execute "updateClass" after "storeValues", not "createNewClass" 我的意思是storeValues函数应该是一个单独的函数,可以从任何其他地方调用,我知道可以通过从“ storeValues”函数执行“ createNewClass”来解决此问题,但是有时我需要执行“ storeValues”之后的“ updateClass”,而不是“ createNewClass”

You can use a callback like this, if your storeValues is not synchronous like in your example: 如果您的storeValues不是像示例中那样同步, 可以使用这样的回调:

$('.tableClassHeader').on('click', '.createClass', function(){
    storeValues(createNewClass);
});

function storeValues(callback){
    cl_year = $('.newClassForm').find('select[name=cl_year]').val();
    cl_course = $('.newClassForm').find('select[name=cl_course]').val();
    callback();
}

function createNewClass(){
    alert(cl_year);
}

If it is synchronous, just calling createNewClass after storeValues is enough. 如果是同步的,则在storeValues之后调用createNewClass就足够了。

What this does is: 这是什么:

  • offers you the ability to pass a function of choice to the storeValues 使您能够将选择的函数传递给storeValues
  • inside storeValues it calls the callback function passed as parameter storeValues内部,它调用作为参数传递的回调函数

If you need to execute your function with a different scope you can use call or apply . 如果您需要在其他范围内执行函数,则可以使用callapply


Another way to do this, without callbacks would be using 没有回调的另一种方法是使用

http://api.jquery.com/promise/ http://api.jquery.com/promise/
http://api.jquery.com/jQuery.when/ http://api.jquery.com/jQuery.when/
http://api.jquery.com/deferred.promise/ http://api.jquery.com/deferred.promise/

Example as seen here http://jsfiddle.net/47fXF/1/ : 如此处http://jsfiddle.net/47fXF/1/所示的示例:

$('.tableClassHeader').on('click', '.createClass', function(){
    $.when(storeValues()).then(createNewClass);
});

function storeValues(){
    var dfd = new jQuery.Deferred();
    setTimeout(function(){
        console.log('storing values');
        cl_year = $('.newClassForm').find('select[name=cl_year]').val();
        cl_course = $('.newClassForm').find('select[name=cl_course]').val();
        dfd.resolve();
    }, 1000);

    return dfd.promise();
}

function createNewClass(){
    alert("trololo");
}

Added the setTimeout to simulate asynchronicity. 添加了setTimeout以模拟异步性。

If your storeValues is making only one ajax request using jQuery, then you can return it directly as shown in the API documentation. 如果您的storeValues仅使用jQuery发出一个ajax请求,则可以直接返回该请求,如API文档中所示。

Also make sure to call resolve() , reject() appropriately. 还要确保适当地调用resolve()reject()

Call like this . 这样打电话。 it first call the storeValues after executes the createNewClass function 在执行createNewClass函数后,它首先调用storeValues

$('.tableClassHeader').on('click', '.createClass', function(){

    storeValues(function() {
        createNewClass();
    });

});

function storeValues(callback){
    cl_year = $('.newClassForm').find('select[name=cl_year]').val();
    cl_course = $('.newClassForm').find('select[name=cl_course]').val();
    callback();
}

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

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