Understanding the Salmon Data Model

Data is consistent with your Salmon web site database.

All data is kept in a database. You are free to organize your tables and columns as you want. Unlike many eCommerce platforms, you are not forced to follow a specific data structure.

Let say you have made a table called items with 3 columns - name, description, and price. When you call the helper JavaScript function e2c_listItems() with relevant parameters, you will be returned some JSON that looks similar to the following example:


The name1, name2, name3, price1, price2, price3, description1, description2, and description3 being replaced by the data that is in the relevant table row.

1) How Behaviors Use Data

The behavior will iterate the JSON data, inside a for (var i=0; var<object.length; i++) loop, and match each key with a DOM element that has an ID key+i, then update this element with that key value.

Naturally we could refactor the e2c_behavior function to a single, common, behavior, but we wish to give beginners expended examples when they are becoming acquainted with e2c.js.

A behavior can be set to use such data in a different way, such as passing it to another JavaScript library, such as ext.js, which would render it into a grid.

Again, we provide preset behaviors in e2c.js for most common ecommerce operations, but you can modify these or create additional behaviors to suit your needs.

2) Limiting Data Size

You can see from the JSON structure and behaviors code that you could set a limit to the number of elements that are to be processed. For example, if you called for a list of items and there are 3,000 products returned, you would not want to show them all at once. If you intend to only show 8 results per page, you can set the max numbers per page var itemsPerPage=8 in e2c.js, or reset it at page load. This would ask Salmon to return only 8 items. You may want to see the pagination() function in e2c.js for how we make such subsequent calls in the Salmon demo store.

The purpose here is to lower the amount of data requested from the database, providing a faster response to the user. However, if you can predict what your users may do next, you could load more products at once, keeping their data into an array or any sort of buffer, then using JavaScript logic dynamically update the DOM on next page click. You would have to modify pagination accordingly, but this is quite simple to achieve. The advantage would be a faster experience for your visitors browsing products.

Note: We could have done this in the demo store, but we wanted to keep the example simple for Salmon beginners.

3) A Flexible Data Model

The Salmon data model is very flexible. Let say that you added a new column to your table items named inStock. Salmon will return the JSON data as below:

{"items:"[{'name":"name1",'description':"description1",'price':"price1", 'inStock':"inStock1"},{'name":"name2",'description':"description2",'price':"price2", 'inStock':"inStock2"},{'name":"name3",'description':"description3",'price':"price3", 'inStock':"inStock3"}...]}

With Salmon, you build and modify your database as you like. If your customer asks you to make changes to the data structure, for example, adding inStock information, you don't have to modify all your scripts as with server-side PHP eCommerce software. You just add the DOM element with id="e2cinStock" to your web page.

Did We Not Say Salmon is All About Simplicity?

Essentially, Salmon has a generic server-side API, such as e2cList, to manage data in a very simple way. When you call the apiname e2cList though Ajax, using the helper e2c_listItems, Salmon will return the data as is from the database.

4) Granulated Queries to Select Data

You can provide your helper function the Ajax apidata string with parameters such as as limit:integer, orderby:string as well as price:>10<100, which returns rows with column price more then 10 and less than 100).

5) Data Access Rules

Since Salmon lets you access data from client-side calls, this could lead to security breaches, as any client-side script could request sensitive data. Salmon has mechanisms to prevent this, parsing all queries against SQL injection, and only returning data for authorized tables. For example, you are probably fine with anyone seeing your products, but do want to restrict access to your orders. You may also set which columns are excepted from a table in the returned data.

Salmon Has a Data-oriented API, Easing Your Access to, and Management of, Data

The primary Salmon server API calls for data management are:

- e2cCreate
- e2cDelete
- e2cList
- e2cGet
- e2cUpdate

These API names, which are to be passed to Ajax enabled e2c_getData() by an helper function, allow you to do whatever data related logic you want on client-side.

Furthermore, not only can you cache data into arrays or buffers of any sort on the client-side, Salmon provides server-side memory caches to accelerate data related processes.