REST API
object graph api
list/show function support
searh/dynamic views using solr
Transaction support

<html>
<head>
<title>Index</title>
<style type="text/css">
@import url("css/ext-all-gray.css");
@import url("css/style.css");
</style>
<!--<script type="text/javascript" src="ext-all-debug-w-comments.js"></script>
<script type="text/javascript">
Ext.onReady(function () {

Ext.define('mine', {
extend: 'Ext.data.Model',
fields: [
{name: '_rev', type: 'string'}
]
});

Ext.define('address', {
extend: 'mine',
fields: [
{name: 'city', type: 'string'}
]
});

Ext.define('Order', {
extend: 'mine',
fields: [
{name: 'name', type: 'string'},
{name: 'email', type: 'string'},
{name: 'phone', type: 'string'},
],
associations: { type: 'hasOne', model: 'address' }
});

store = Ext.create('Ext.data.Store', {
autoLoad: true,
model: "Order",
proxy: {
type: 'rest',
url: 'list.js',
reader: {
type: 'json',
root: 'users'
}
}
});

Ext.create('Ext.data.Store', {
storeId:'simpsonsStore',
model: "Order",
data:{'items':[
{ 'name': 'Lisa', "email":"lisa@simpsons.com", "phone":"555-111-1224", "address" : { city :
'NY' } },
{ 'name': 'Bart', "email":"bart@simpsons.com", "phone":"555-222-1234" },
{ 'name': 'Homer', "email":"home@simpsons.com", "phone":"555-222-1244" },
{ 'name': 'Marge', "email":"marge@simpsons.com", "phone":"555-222-1254" }
]},
proxy: {
type: 'memory',
reader: {
type: 'json',
root: 'items'
}
}
});

Ext.create('Ext.grid.Panel', {
title: 'Simpsons',
store: Ext.data.StoreManager.lookup('simpsonsStore'),
columns: [
{ text: 'Name', dataIndex: 'name' },
{ text: 'Email', dataIndex: 'email', flex: 1 },
{ text: 'Phone', dataIndex: 'phone' },
{ text: 'City', renderer: function(value,m,r) {
return value.city;
} }
],
height: 200,
width: 400,
renderTo: Ext.getBody()
});
});

</script>-->
<script type="text/javascript">

var models = {
order : {
type : 'order',
associations : [
{ name : 'customer', model : 'customer' },
{ name : 'tags', model : 'tag' },
{ name : 'product', path : 'orderitem', model : 'product' }
]
}
};

var obj = {
_id : 'OR001',
type : 'order',
orderitem : [
{
qty : 2,
product : {
_id : 'PR001',
name : 'Product 1'
}
},
{
qty : 3,
product : {
_id : 'PR002',
name : 'Product 3'
}
}
],
customer: {
_id: 'CU001',
name : 'Kiruba'
},
tags : 'TA001',
{
_id : 'TA002',
name : 'TAG 2'
}

};

function dbin(data, name){
if(Array.isArray(data)){
var newarray = [];

data.forEach(function(item){
var a = dbin(item, name);
newarray.push(a)
});

return newarray;

} else if(typeof(data) === "object"){
if(data._id){
return data._id;
}
else if(name){
if(data[name]._id)
data[name] = data[name]._id;
return data;
}
} else {
return data;
}
}

function dbout(data, name){
if(Array.isArray(data)){
var newarray = [];

data.forEach(function(item){
var a = dbout(item, name);
newarray.push(a);
});

return newarray;

} else if(typeof(data) === "object"){
if(name){
if(data[name])
data[name] = dbout(data[name]);
return data;
}
} else {
var o = items[data];
return o ? o : data;
}
}

obj.relatives = {};

models"order".associations.forEach(function(association) {

var val = null;

if(!obj.relatives[association.model]){
obj.relatives[association.model] = [];
}

if(association.path){
val = obj[association.path] = dbin(obj[association.path], association.name);

if(Array.isArray(val)){
val.forEach(function(item){
obj.relatives[association.model].push(item[association.name]);
});
} else {
obj.relatives[association.model].push(val);
}

} else {
val = obj[association.name] = dbin(obj[association.name]);

if(Array.isArray(val)){
val.forEach(function(item){
obj.relatives[association.model].push(item);
});
} else {
obj.relatives[association.model].push(val);
}
}

});

console.log(JSON.stringify(obj));
var rtypes = 'customer','tag';

var neededkeys = [];
var neededassociations = [];
models"order".associations.forEach(function(association) {
var need = false;
need = rtypes.some(function(el){return el === association.model;});
if(need){
neededkeys = neededkeys.concat(obj.relatives[association.model]);
neededassociations.push(association);
}
});

console.log(JSON.stringify(neededkeys));

var items = {
'PR001' : {
_id : 'PR001',
name : 'Product 1'
},
'CU001' :{
_id: 'CU001',
name : 'Kiruba'
},
'TA001' : {
_id : 'TA001',
name : 'TAG 1'
},
'TA002' : {
_id : 'TA002',
name : 'TAG 2'
}
};

neededassociations.forEach(function(association){
if(association.path){
obj[association.path] = dbout(obj[association.path], association.name);
} else {
obj[association.name] = dbout(obj[association.name]);
}
});

delete obj.relatives;

console.log(JSON.stringify(obj));

</script>
</head>
<body>
<button onclick="a();" value="a"/>
</body>
</html>

Last edited Dec 4, 2012 at 7:35 PM by kirubasankars, version 2

Comments

No comments yet.