Solved

Arrays and functions in JQuery

Posted on 2013-12-16
6
263 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

757 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

22 Experts available now in Live!

Get 1:1 Help Now