SyncData
SyncData is a little Browserify | Node module which manage inside a JavaScript Array + CRUD functionalities + Cache ID through CUID by Eric Elliot. When you use the CRUD functionalities, those trigger the actions to the server via Ajax and modify the JavaScript Array, if the Ajax request fails, you have an observable to take the necessary actions over the JavaScript Array.
Implementation
Browser
var syncdata = syncdatasyncData = ;
Node
npm install syncdata
var syncdata =syncData = ;
setRoutes
It is important to set the server routes where you will send & request information to the server.
setRoutes(params)
params: CREATE | READ | UPDATE | DELETE
syncData;
setObservable
You can observe the CRUD action, item managed and Ajax result.
function setObservable(fn)
returns: route (CREATE - READ - UPDATE - DELETE) | response (Ajax Result) | item (JS Object to include)
syncDataobserve;
Internal Array
You can access the internal JS Array or get a Clone of the Array.
function data(params)
params: clone (true/false)
returns: JS Array
syncDatadata clone: true ; // []
Create
Add an item to the internal array and send the ajax request to the server.
function create(params, callback)
params: item (JS Object to include) | async (true/false) | method (POST/GET)
returns: server response
observable result:
item ( {"Name":"Mochi","Age":"27", cuid: "ci71rh0fk00003352ijew3d28"} )
response ( Ajax response )
route ( CREATE )
var item = Name: 'Mochi' Age: 27 ;syncData;
Read
Fill the internal array through an ajax request to the server, it should return an array (JS/JSON).
function read(params, callback)
params: item (JS Object to include) | async (true/false) | method (POST/GET) | data (Ajax data)
returns: server response
observable result:
item ( {"Name":"Mochi","Age":27,"cuid":"ci71rh0fk00003352ijew3d28"} )
response ( Ajax response )
route ( READ )
syncData;
Update
Pass the item with the changes and it is updated the internal array and send the ajax request to the server. The item should contain the cuid property.
function update(params, callback)
params: item (JS Object to include with cuid property) | async (true/false) | method (POST/GET) | data (Ajax data)
returns: server response
observable result:
item ( {"Name":"Mochi","Age":27,"cuid":"ci71rh0fk00003352ijew3d28"} )
response ( Ajax response )
route ( UPDATE )
var item = syncDatadata0;itemName = 'Panfilo';itemID = 1;syncData;
Delete
Pass the cuid of the item to delete and it is deleted in the internal array and send the ajax request to the server. The item should contain the cuid property.
function delete(params, callback)
params: cuid (cuid of the item to delete) | async (true/false) | method (POST/GET) | key (Ajax Send just one Property Field of the item e.g: 'ID')
returns: server response
observable result:
item ( {"Name":"Mochi","Age":"27","ID":1,"cuid":"ci71rh0fk00003352ijew3d28"} )
response ( Ajax response )
route ( DELETE )
var item = syncDatadata0;syncData;
Select
Search an item by cuid in the internal array (No ajax method involved)
function select(params)
params: cuid (cuid of the item to delete) | clone (true/false)
returns: object
var item = syncData;console; // {"Name":"Mochi","Age":27,"cuid":"ci71rh0fk00003352ijew3d28"}
Extension ext()
It is an object which contains sub objects with different purposes, for example: eVal, it contains functions like: validates isNumber, isBlank, isEmail, another example... eDate, this contains: monthName, dayName and so on.
eDate()
eDate().monthName()
function monthName(params)
params: month (month number) | language (en/es)
returns: string
syncData; // Current Month Name in EnglishsyncData; // Current Month Name in SpanishsyncData; // Junio
eDate().dayName()
function dayName(params)
params: day (day number) | language (en/es)
returns: string
syncData; // Current Day Name in EnglishsyncData; // Current Day Name in Spanish
eDate().shortDate()
function shortDate(params)
params: value (date) | format (Date Format)
returns: string
syncData; // Example: 03/12/2015syncData; // Example: 12/03/2015
eDate().middleDate()
function middleDate(params)
params: value (date) | format (Date Format)
returns: string
syncData; // Example: March 12, 2015syncData; // Example: December 3, 2015
eDate().largeDate()
function largeDate(params)
params: value (date) | format (Date Format)
returns: string
syncData; // Example: March 12th, 2015syncData; // Example: December 3rd, 2015
eDate().longDate()
function longDate(params)
params: value (shortDate string) | format (Date Format)
returns: string
syncData; // Example: 20150312syncData; // Example: 20151203syncData; // Example: 20150125
eDate().fromDate() / eDate().fromHours() / eDate().fromMinutes()
function fromDate(params)
params: value (shortDate string) | format (Date Format)
returns: string
syncData; // Example: 19 hours agosyncData; // Example: 3 days agosyncData; // Example: in 7 months
eDate().addDays()
function addDays(params)
params: value (shortDate string) | format (Date Format) | days (number of days to add)
returns: string
syncData; // Example: 03/13/2015syncData; // Example: 10/04/2015syncData; // Example: 03/11/2015
eDate().addMonths()
function addMonths(params)
params: value (shorDate string) | format (Date Format) | months (number of months to add)
returns: string
syncData; // Example: 04/12/2015syncData; // Example: 04/10/2015syncData; // Example: 11/03/2015
eDate().addYears()
function addYears(params)
params: value (shortDate string) | format (Date Format) | years (number of years to add)
returns: string
syncData; // Example: 03/12/2016syncData; // Example: 10/03/2016syncData; // Example: 03/10/2016
eDate().now()
function now()
returns: string
syncData; // Example: 2015-03-12, 7:14:09 pm
eFormat()
eFormat().addMiles()
function addMiles(params)
params: value (number/string)
returns: string
syncData; // 1,000syncData; // 1,000
eFormat().removeMiles()
function removeMiles(params)
params: value (string)
returns: float
syncData; // 1000
eVal()
eVal().isNumber()
function isNumber(params)
params: value (number/string)
returns: true/false
syncData; // truesyncData; // false
eVal().isBlank()
function isBlank(params)
params: value (number/string)
returns: true/false
syncData; // falsesyncData; // truesyncData; // true
eVal().isDate()
function isDate(params)
params: value (shortDate string) | format (Date Format)
returns: true/false
syncData; // truesyncData; // truesyncData; // false
eVal().isEmail()
function isEmail(params)
params: value (email string)
returns: true/false
syncData; // truesyncData; // false
eSecurity()
eSecurity().encrypt()
function encrypt(params)
params: value (string to encrypt) | token (Hash string)
returns: { encrypted, token }
syncData; // Object {token: "ci76xqrxp000032566nbk822h", encrypted: "U2FsdGVkX19zqWXY+GFhqCXbu7BGW1HFqC5Sgt/A+2c="}syncData; // Object {token: "123", encrypted: "U2FsdGVkX19ff8dbBucraT7HXm3nW10i1Z7TOLMFQi0="}
eSecurity().decrypt()
function decrypt(params)
params: value (string to decrypt) | token (Hash string)
returns: { decrypted, token }
syncData; // Object {token: "123", decrypted: "Steven"}
jQuery, cuid, moment, crypto - Global Scope
SyncData attached to the window or global objects the jQuery and Cuid literal objects
global.$ / window.$
global.cuid / window.cuid
global.moment / window.moment
global.cryptojs / window.cryptojs