Forum Stats

  • 3,770,454 Users
  • 2,253,116 Discussions
  • 7,875,462 Comments

Discussions

CRUD Question

3123923
3123923 Member Posts: 4
edited Nov 19, 2015 2:56AM in Oracle JET

Hi, i am exciting to learn more Oracle Jet.

I am using Quickstart template to build a simple listing. So the first thing i need to do is try insert table by refering

Oracle JET - CRUD - CRUD Using Table

Everything is work fine same like the example as the link above, but i not sure how the way to insert into database. I am calling ajax to insert data into database but it is seems like weird for collection and model here.

For my concept is insert must a Json url with insert purpose. For the example the link above, i no idea how to tell collection the JSON URL in collection.create.

This is my code is :

            // Our custom implementation of all the CRUD operations, replacing the default Ajax implementation

            function sync(method, model, options) {

                switch (method) {

                    case "read":

                        // Handle collection fetches

                        if (model instanceof oj.Collection) {

                            // Call the success option with the data

                            var data = getStorageData();

                            if (options['success']) {

                                options['success'](data);

                            }

                        }

                        else {

                            // Must be a Model fetch: look for it by ID and return that

                            var data = getStorageData();

                            var employees = data.items;

                            var idAttribute = model.idAttribute;

                            for (var i = 0; i < employees.length; i++) {

                                if (employees[i][idAttribute] === model.id) {                       

                                    if (options['success']) {

                                        options['success'](employees[i]);

                                    }

                                    break;

                                }

                            }

                        }

                    break;

                    case "create":

                        // Create a new model and add it to the stored data

                        var data = getStorageData();           

                        var employees = data.items;

                        model.attributes[model.idAttribute] = ++ maxId;

                        model.set("first_name", null);

                        model.set("last_name", null);

                        model.set("email", null);

                        employees.push(model.attributes);

                        setStorageData(data);

                        if (options['success']) {

                            //console.log("sync message:"+options['success']);

                            options['success'](employees[employees.length-1], null, null);

                        }

                    break;

                }

                // We don't have an xhr object: not Ajax

                return {};

            };

Then the part of insert code in View Model is

// Insert Query Over Here

                // Create handler

                self.addEmployee = function (formElement, event) {

                    var recordAttrs = {

                        "FIRST_NAME": formElement.elements[0].value,

                        "LAST_NAME": formElement.elements[1].value,

                        "EMAIL": formElement.elements[2].value,

                        "HIRE_DATE": '16-FEB-2015',

                        "JOB_ID": 'SH_CLERK'                                               

                        };

                  

                       

                    var retObj = {};   

                    //create is a function that push attribute into session and table

                    this.EmpCol().create(recordAttrs,                      

                        {wait:true,  // waits for server to respond with 200 before adding newly created model to collection  

                        'contentType': 'application/vnd.oracle.adf.resource+json',

                        success: function (response) {    //before AJAX called, that is no error at here.                                                  

                            //console.log(response);

                            var data = ko.toJSON(recordAttrs);

                              //MY INSERT way

                            $.ajax({

                                url: "http://insert_json_url/XD",

                                type: "POST",

                                data: data,

                                //datatype: "json",

                                processData: false,

                                contentType: "application/json",

                                success: function (result) {

                                    //console.log(result);

                                    alert(result + "INSERT SUCCESSFULLY");

                                    //$('#message').html("INSERT SUCCESSFULLY");

                                    //window.location = employeeListing + "/" + result;

                                    //window.location = 'index.html?root=employeeListing';

                                },

                                error: function (jqXHR, textStatus, errorThrown) {

                                    var errorMessage = '';

                                    //$('#message').html(jqXHR.responseText); //full website error of APEX

                                    $('#message').html(jqXHR.responseText);

                                }

                            });   

                               

                             

                        },

                        error: function (jqXHR, textStatus, errorThrown) {

                            //console.log('Error in Create: ' + textStatus);                           

                            console.log('error callback');

                            // this error message for dev only

                            alert('There was an error. See console for details');

                            console.log(jqXHR);

                        }

                    });

                };

          // this is my way to display listing

           $.getJSON(self.serviceURL,

                function (data) {

                    // Get our read JSON into browser session storage, if it's not already there

                    if (noData()) {

                        setStorageData(data);

                    }

                    else {

                        data = getStorageData();

                    }

                    setMaxId(data, 'employee_id');

                    self.EmployeeData(new oj.PagingTableDataSource(new oj.CollectionTableDataSource(self.EmpCol())));

                });  

Any idea or improvement for insert on it?