views:

220

answers:

2

I have 2 tables in DB:

1- Element:

  • ElementID
  • ElementName

2- Methods:

  • MethodID
  • ElementID
  • MethodData

So there is a one to many relation between Elements and Methods,

I load these tables data using asp.net,

What i want to do is to send these data to my javascript, and javascript will do some functions on these data.

For example will loop through all elements and get each element methods and do some stuff with each method data.

I tried to make this as classes in jaascript but found my self wrote a lot of things,

First 2 classes, for elements and methods, then 2 arrays 1- for elements 2- for methods

And inside the methods array i wrote this:

this.findByElementId = function(elementId) {
    var result = [];
    for (var i = 0; i < this.methods.length; i++) {
        if (elementId === this.methods[i].methodElement) {
            result.push(this.methods[i]);
        }
    }
    return result;
}

which made my code slow.

My Question is how to represent this relational structure in javascript for more professional code and faster in processing ?

A: 

I've never tried this stuff myself, but people have written LINQ-esque libraries for JavaScript, like JSINQ. (That page links to more JS LINQ libraries at the bottom of the page, too.)

It claims that you can "write arbitrarily complex queries against JavaScript arrays, DOM node lists or your own enumerable types", which sounds like what you want.

Daniel Lew
good link, but i am afraid that this may make my code slower, but its nice, will try
Amr ElGarhy
If you need blazing speed, you have to code to your specific situation; general solutions will rarely do better, because you're usually sacrificing speed for utility.
Daniel Lew
So if i will not use a utility like this, my code is good?
Amr ElGarhy
A: 
function Element() {
this.id;
this.type;
this.methods = new Array();

}

function Method() {
    this.id;
    this.methodType;
    this.methodData;
    this.methodElement;
    this.methodRank;
}
Amr ElGarhy