Setting up Local JSON Using gmethod

Though, in production, Salmon client-side JavaSscript will interact with Salmon server though Ajax calls, for development and maintenance purposes, you may need to setup a local JSON data array. This is extremely simple as we will explain below.

Here are some reasons for setting up a local JSON data array:

- You don't have access to an Salmon server, but wish to develop a feature for it.
- You are debugging and need to verify your code against valid data.
- You cannot access some server data due to Salmon data access rules.

There may be other reasons, but regardless, in all of these cases, you will need to emulate a data array that would otherwise have been returned to you by an Salmon server.  You can do so by setting up a local JSON array with the gmethod parameter.

Salmon client side JavaScript, mainly e2c.js, makes Ajax calls to the server though the function e2c_getData(). This function has a parameter, gmethod, which can be set to use local JSON.

By default, gmethod is set to 1, and e2c_getData() will attempt to get data from the Salmon server. If gmethod is set to 2, e2c_getData() will load the data from your local JSON array.

You can verify this by yourself in e2c.js, function e2c_getData(...):

else if (gdmethod == 2) {    //gdmethod==2 for passing JSON string to behavior
var jsonData = GetJsonData(bcode);
e2c_behavior(bcode, 's', jsonData, '', subbcode, gdmethod);
}

Building Your Custom JSON Array

When the e2c_getData() gmethod parameter is passed as 2, it will invoke the GetJsonData() function. You can write your own GetJsonData() function to return any JSON data that you wish, as demonstrated in the example below.  It only takes a few minutes.

Note that in passing a bcode parameter, you can have your function use various sets of preset local JSON data. This allows you to reuse and switch from various sets of data, and even build large data collections.

Below is an example to demonstrate a custom GetJsonData() function.

function GetJsonData(bcode) { 
var jsonData = { "items": [{ "reference": "101", "itemID": "101", "store": "1", "vendor": "1", 
"category": "23", "name": "Blue     Necklace", "short_description": "", 
"description": "", "price": "1500", "currency": "USD", "quantity": "2", "pics": 
"101.jpg", "featured": "yes" }, 
{}] } 
return jsonData; 
} 

Note the empty {} before the ending square bracket of the JSON data, shortly after the 'yes" data in the example above. This is necessary for Salmon.

Now, when your code calls e2c_getData() with gmethod set to 2 it will be have returned to it the data that you set in GetJsonData().

You can verify this by simply making a function that will use gmethod set to 2:

function e2c_mycustomFunction(){
var turl = "http://yoursite.com/e2c/e2cList";                   
var subbcode = 1;
var bcode = "20"; 
var apiname = "e2cList"; var apidata = "t=items&w=featured:yes;limit:10";  featuredItemsPerPage = 10;
e2c_getData(apiname, apidata, bcode, subbcode, 2, false, turl);  
					//note the gmethod here set to 2
          } 

and call it on document.ready in your HTML page HEAD section, as below.

<script type="text/javascript">
 $(document).ready(function (ui) {
           e2c_mycustomFunction();
 }); 
 </script>

Despite the target URL in the e2c_mycustomFunction is http://yoursite.com/e2c/e2cList, because the gmethod is set to 2, it will return data from your local JSON array which you defined previously in GetJsonData().

You may create various set of local JSON data, and using bcode you can decide which one to use at any given time. For for sake of clarification, here is an example of multiple sets of local JSON data in a GetJsonData() implementation:

function GetJsonData(bcode) {   
var jsonData;     
if(bcode==1){
jsonData = { "items": [{ "reference": "101", "itemID": "101", "store": "1", "vendor": "1", 
                    "category": "23", "name": "Blue     Necklace", "short_description": "", 
                    "description": "", "price": "1500", "currency": "USD", "quantity": "2", "pics": 
                    "101.jpg", "featured": "yes" }, 
			{}] }       
}  
if(bcode==2){
jsonData = {"orders":[ {"reference": "1368385217412", "orderID": "", 
			"ownerID": "1", "productID": "107", "status": "awaitingPayment", 
			"title": "Green Necklace", "quantity": "4", "price": "1200", 
			"currency": "USD", "total": "4800", "share": "", "paid2owner": "", "clientID": 
			"33", "shipdate": "", "shipcomp": "", "shipref": "", "paytype": "2", 
			"paydata": "", "payshipadd": "184:184", "ownerlogin": ""}, 
			{"reference": "1368385217412", "orderID": "", "ownerID": "1", "productID": 
			"105", "status": "awaitingPayment", "title": "Gold & Pearls 
			Necklace", "quantity": "3", "price": "2805", "currency": "USD", 
			"total": "8415", "share": "", "paid2owner": "", "clientID": "33", "shipdate": 
			"", "shipcomp": "", "shipref": "", "paytype": "2", "paydata": "", "payshipadd": 
			"184:184", "ownerlogin": ""}, {} ]}      
}    
if(bcode==3){      
            // your other JSON data       
}
			.... //add as much as you want 
return jsonData; 
}

Furthermore, you may also have your local data variable jsonData populated from another function that you wrote, which may contain previous local JSON sets that you had created and saved, or from any other data source which can be linked to your code. It's your code - you can do what you want.

The gmethod option is for convenience. In addition to offering the possibility for local JSON data, it may also open further possibilities for development and maintenance options.  We will leave that to your imagination! :)

Getting JSON Data Structures

We are often asked how does one know about the structure of the JSON data for local development purposes.

You could ask that the admin give you the database table structure, but there may be good reasons that they would not want to disclose this. In any case, solving the issue on your own is better, especially as Salmon provides mechanisms to do so.

If the database table on the server already exists, you can query it directly by building the query string as any other Salmon client-side function does.

For example, by making a query such as http://demo.e2cserver.com/e2c/e2cGet?t=items&r=x, and entering it into your browser, if the value x does not exist, we would get an empty data array such as below:

{"reference": "null", "itemID": "null", "store": "null", 
"vendor": "null", "vendorlogin": "null", "category": "null", "name": 
"null", "short_description": "null", "description": "null", "price": 
"null", "currency": "null", "quantity": "null", "pics": "null", 
"featured": "null"} 

Due to Salmon data access rules, this technique may not work for all tables. This depends on the rules set by the Salmon admin for a said Salmon powered server. You may be able to access some data, but not all.

What you can do is get the structure by using a query that already works in the JavaScript application that gets data from that table. For example, a query such as http://demo.e2cserver.com/e2c/e2cGet?t=items&r=102 on our demo store would return:

{"reference": "102", "itemID": "102", "store": "1", "vendor": 
"1", "vendorlogin": "First Blend LTD", "category": "7", "name": 
"Blue Ear Rings", "short_description": "", "description": "", 
"price": "1850", "currency": "USD", "quantity": "10", "pics": 
"102.jpg", "featured": "yes"}

With this, you will get enough data to work with.

Now, if the database table on the server does not exist, it likely means that you are developing a new feature. Unless you have been asked to follow a particular structure, then you are free to create your own JSON data structure.

You would then provide that structure along the code you developed for later table creation.

For example, let say we are making a new feature that writes Hello Word on the page, and keeps the number of times, and the date and time it was run, we could make our own JSON data structure as below:

{"reference": "", "counter": "", "timestamp": ""}

and populate it with some test data:

{ "myTableName":[{"reference": "1", "counter": "10", "timestamp": 
"1362268800"} ,{"reference": "2", "counter": "23", "timestamp": 
"1362355200"} ,{"reference": "3", "counter": "130", "timestamp": 
"1365033600"} , ..., {} ]}

In fact, even if you have not been given any data structure, nothing stops you from creating your own and testing your code against it.