jose
jose

Reputation: 1562

ajax request - update record without form

To do a record update I usually use a CRUD and a store like this:

        storeId: 'storeId',
        model: 'model',
        pageSize: 10,
        autoLoad: true,

        proxy: {
            type: 'ajax',

            actionMethods: {
                create: 'POST',
                read: 'POST',
                update: 'POST',
                destroy: 'POST'
            },

            api: {
                create: 'php/crud.php?action=create',
                read: 'php/crud.php?action=read',
                update: 'php/crud.php?action=update',
                destroy: 'php/crud.php?action=destroy'
            },

            reader: {
                type: 'json',
                rootProperty: 'net',
                totalProperty: 'total',
                successProperty: 'success'
            },

            writer: {
                type: 'json',
                writeAllFields: true,
                encode: true,
                rootProperty: 'net'
            }
        }

Logic for the update:

    var record = form.getRecord();
    record.set(values);

    store.sync({
        success: function () {

        },

        failure: function () {

        },

        callback: function () {
        },

    });

Problem: just change the value of a database column: YES / NO

Instead of removing a record with 'destroy', I want to just disable it with 'update' a database table column form NO to YES.

In this case I have no form; just a delete button.

I tried without success:

    store.proxy.extraParams = {
           sub_case: 'change_delete_state',
           id_lista: id_lista
    },

    store.sync({
        success: function () {

        },

        failure: function () {

        },

        callback: function () {
        },

    });

And:

    Ext.Ajax.request({
        url: 'php/crud.php?action=update',
        params: {
           sub_case: 'change_delete_state',
           id_lista: id_lista
        },
        success: function () {
            store.commitChanges();
        },

        failure: function () {

        },

        callback: function () {
        },
    });

I would appreciate suggestions to solve this problem.

EDITED:

Solved like this:

    api: {
        destroy: 'php/crud.php?action=destroy'
    },

cliente-side logic:

        // soft delete
        store.proxy.extraParams = {
           sub_case: 'change_delete_state',
           id_lista: id_lista
        },

        //or
        // hard delete
        store.proxy.extraParams = {
           id_lista: id_lista
        },

        store.remove(record);
        store.sync({...});

server-side (PHP):

case "destroy":{
    $record = stripslashes($_POST['net']);
    $data = json_decode($record);

    $id_lista = $data->{'id_lista'};

  if($_REQUEST['sub_case'] == "change_delete_state"){      
        $sqlQuery = "UPDATE ...";
        (...)
    }else{
        $sqlQuery = "DELETE ...";
        (...)
      }
}

Upvotes: 0

Views: 322

Answers (1)

Alexander
Alexander

Reputation: 20224

You are well off if you hide the database logic on the server-side as far as possible.

So, if you only have one way to get rid of a record in the front-end, and the record should be removed from the store, you would just exchange the destroy api of that one "special" store with a special api:

        api: {
            create: 'php/crud.php?action=create',
            read: 'php/crud.php?action=read',
            update: 'php/crud.php?action=update',
            destroy: 'php/crud.php?action=hide' // hide the record using a SQL update, and remove it from the client-side store; don't remove from server
        },

Of course you would have to insert special logic into the crud.php to do the special processing on action=hide.

If you want to have two different ways, one for a "hard delete" and one for a "soft delete", but both should be removed from the store, things become a bit more complicated. You would then need a special boolean "flag" in your record that is processed server-side.

E.g.

fields:[{
    name:'hardDelete',
    type:'bool',
    defaultValue:false
}]

and then you would do sth. along

if(hardDelete) record.set("hardDelete",true);
store.remove(record);
store.sync(

and on the server side you would have to read that flag and act according to its value.

Upvotes: 1

Related Questions