Solved

Arrays and functions in JQuery

Posted on 2013-12-16
6
270 Views
Last Modified: 2013-12-18
We are using client side technologies Kendo UI,knockout and Jquery for our application , in one of the pages we have a grid that will list out all the books with book name,author name, rate /price etc with default sort on rate column.I have following piece of JQuery  code in bookModel.js file and am finding difficult to understand  and  can anyone briefly explain  the code . (which commented with question marks)

var bookresult={

// Properties and functions

Booksearchcriteria:

defaultSort:"rate",

softField:"rate"

sortAsc:true,

booksearchRank:0,

sort:function(field){

//some logic

var tempgroup=new Array();

//some logic

[b]//why we need to pass this.get ?[/b]

tempgroup.sort(dataModels.BooksearchComparator(this.get("sortAsc"),this.get("sortField"),this.get("defaultSort")));  

},

};

Open in new window


//function defined

dataModels.BooksearchComparator=function(sortAsc,sortField,defaultSort) {

var bookcomparator=fucntion(x,y) {     // ? 

var valueXRank=a["booksearchRank"];    // ?
var valueYRank=b["booksearchRank"];

if(typeof(a.booksearchRank)=='undefined') {   // ?
valueXRank=0;
valueYRank=0;
}
var valueX=a[sortField]; //?
var valueY=b[sortField];

};

return bookcomparator;

}

Open in new window

0
Comment
Question by:ksd123
  • 3
  • 3
6 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39721309
You have an error on that JSON line 5.
Booksearchcriteria property doesn't have a value neither ends with a comma.
So starting with this you'll have javascript errors.

dataModels.BooksearchComparator = function (sortAsc, sortField, defaultSort) {

    var bookcomparator = fucntion(x, y) { // function have a spelling error, should be function, not fucntion

        var valueXRank = a["booksearchRank"]; // get a property value from a json object
        var valueYRank = b["booksearchRank"];

        //if (typeof (a.booksearchRank) == 'undefined') { // this is no a propper way
        // undefined is actually a type in javascript. 
        // It differs from null. 
        //    Null is when the property exists but is not assigned
        //    undefined is when the property doens't even exist
        
        if (a.booksearchRank === undefined) { // this is correct
            valueXRank = 0;
            valueYRank = 0;
        }
        
        var valueX = a[sortField]; // again, gets the sortField property value from a. This is the same as a.sortField
        var valueY = b[sortField];

    };

    return bookcomparator;

}

Open in new window

0
 

Author Comment

by:ksd123
ID: 39721415
Thank you for the quick response.

1)What is the diff b/w passing params to a function  using  this.get("sortAsc") and just
sortAsc

tempgroup.sort(dataModels.BooksearchComparator(this.get("sortAsc"),this.get("sortField"),this.get("defaultSort")));  

2)can you briefly explain below two lines of code .It looks like function within a function?

dataModels.BooksearchComparator = function (sortAsc, sortField, defaultSort) {

    var bookcomparator = fucntion(x, y) {
0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 400 total points
ID: 39721446
1) I'm a bit lost with that: this.get("sortAsc")
Does that actually work?

To get the value from a JSON object you have 2 options:
this.sortAsc
this["sortAsc"]

this.get("sortAsc") should throw an error unless there's a get() function somewhere.

2) it is in-fact a function within a function.
This is something normal in javascript. Basically we're assigning bookcomparator variable value as a function.

functions in javascript are first-class citizens, (I don't like this comparison but usually makes it easier to explain) much like classes on OOP languages.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:ksd123
ID: 39723586
Thank you.I have two more questions.

1)Below is my bookModel.js file, The first function has new keyword and remaining all functions does not have new keyword. Please comment

2)In the bookModel.js file, I want to declare a variable that will set to boolean value and  should  be available to all the functions in this file, where and How should I declare this?

[b]bookModel.js[/b]

 dataModels.bookSearch=new function() {  //function
 //No code in this function
 };

 dataModels.bookSearch=function() {         //function

 //code in this function
 };

 dataModels.bookSearch.refreshdata=function() {  //function
 
 //code in this function

 };

Open in new window

0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39723832
I will comment but I think you should read at least these books:
http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742/ref=pd_bxgy_b_img_y
http://www.amazon.com/JavaScript-Patterns-Stoyan-Stefanov/dp/0596806752

After read this:
http://www.amazon.com/Performance-JavaScript-Faster-Application-Interfaces/dp/059680279X/ref=sr_1_1?s=books&ie=UTF8&qid=1387285584&sr=1-1&keywords=high+performance+javascript

And you'll have the knowledge needed to answer all the questions you have now.

I can't answer this right now, give me a couple of hours and I'll be back.

Cheers!
Alex
0
 

Author Closing Comment

by:ksd123
ID: 39726452
Thank you for your time
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now