Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Arrays and functions in JQuery

Posted on 2013-12-16
6
Medium Priority
?
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to dynamically set the form action using jQuery.

715 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