How to copy records from a store and send them by Ajax.

There are many ways of doing this, here is one in ExtJs by Sencha.


function copyStoreData(store) {
var data = store.getRange();
var len = data.length;
var newData = [];

for(i = 0; i < len; i++) {
newData.push(data[i].data);
}

return newData;
}

if you need to appy a filter just do like this, or even in json.


function copyStoreData(store, filter) {
var data = store.getRange();
var len = data.length;
var newData = [];

for(i = 0; i < len; i++) {
if(data[i].get('field') !== filter) {
newData.push(data[i].data);
}
}

return Ext.encode(newData);
}

New getters and setters in Es6 JavaScript.

This is a simple example on how to implemet getters and setters in Es6 JavaScript.


class Point {
constructor(x, y) {
this._x = x;
this._y = y;
}
get X() {
return this._x;
}
get Y() {
return this._y;
}
set X(x) {
this._x = x;
}
set Y(y) {
this._y = y;
}
}

var p = new Point(10, 20);

p.X;

And now how to do it without class system.


function Point(x, y) {
this._x = x;
this._y = y;

this.getX = function() {
return this._x;
};

this.getY = function() {
return this._y;
};

this.setX = function(x) {
this._x = x;
};

this.setY = function(y) {
this._y = y;
};
}

var p = new Point(10, 20);

p.getX();

Function to find a value inside Array JavaScript.

This is a simple find() function to search a value inside an array, returns -1 if not found.


function find(array, value) {
if(array.indexOf) {
return array.indexOf(value);
}
for (var i = 0; ++i < array.length;) {
if(array[i]===value) {
return i;
}
}
return -1;
}
var arr = [1,2,3,4,5,6];
var check = find(arr, 5);

if(check !== -1) {
console.log('Found with the val of '+arr[check]);
} else {
console.log('Not found');
}

Simple buffer implementation in JavaScript.

This is my implementation ,to a small project that I had, of an simple buffer to hold some data.


function Buffer(len) {
this.len = len || 10;// the max size of the buffer
this.store = []; // the container to hold data
this.size = 0; // the incremental size of the array
}

Buffer.prototype.add = function(data) {
if(this.store.length < this.len) {
this.store[this.size]=data;
this.size++;
}
};

Buffer.prototype.pop=function() {
var tail = this.size -1;
this.store.splice(tail, 1); // remove
this.size--;
};

Buffer.prototype.dimension = function() {
return this.size;
};