0

i have a program and i was thinking to create an object then a function inside it. is it possible? it's like this var x = function() {....} and i wanted to re use the variable x..

here's the code:

$("#tbl").jqGrid({
    url: '',                            
    datatype: 'local',      
    jsonReader : {              
        root: function(obj) {
        //some codes here

           return root;
        },          
        page: "page",                   
        total: "pageCount",     
        records: "rows",    
        repeatitems:false,  
        id: "0" 
    },

    serializeGridData: function(postData) {
        var jsonParams = {
            .
            .//some codes here
            .

            'sort_fields': postData.sidx
        };

        if (postData.sord == 'desc')
        {
        ..//some codes
        }           
        else
        {
        ...//some codes
        }

        jpar = jsonParams;
        return 'json=' + jsonParams;
    },

    loadError: function(xhr, msg, e) { 
        showMessage('msg error');
    },
    colNames:['ID',...'Type'],      
    colModel:[
    ...//col model
    ],

    rowNum:5,           
    .
    .
    .//some codes here
    loadonce:false,         
    caption: "Main Account Group"
});

i want to get the jsonReader, serializedGridData, and the loadError and create an object function for them. my goal here is to create an object from the functions from the above code. does anybody here knows how to do it?

by the way, what's the difference between methods and functions.? can event can be code as function? thank you all.

10
  • I don't follow -- the code you posted will work fine, what are you asking? Commented Mar 22, 2011 at 7:25
  • Isn't basically the same as your previous question? stackoverflow.com/questions/5374977/… Commented Mar 22, 2011 at 7:28
  • @felix kling. i find it hard to create a separate .js file that is why i'm looking for some options. and maybe this can give me the answer on how to do it in separate file. thanks Commented Mar 22, 2011 at 8:28
  • @ cwolves..yes it works... i just want to know how to separate the code (it's events) and make it as a function. then maybe i could just call them by variable Commented Mar 22, 2011 at 8:30
  • 1
    I see that you use datatype: 'local' in the case NO requests to the server will be done. So the serializeGridData method will never called. How you fill the data in the grid? You defined jsonReader. So you probably insert the data manually with addJSONData it is not a good way in the most cases. Could you insert more full JavaScript code, so that one could understand more your code? Commented Mar 22, 2011 at 10:23

1 Answer 1

1

This one has been really helpful to me in the past =)

Hope it helps http://www.phpied.com/3-ways-to-define-a-javascript-class/

function jqFunctions() {
    this.serializeGridData = function(postData) {
        alert(postData);
    }
}

new jqFunctions().serializeGridData("hello");
Sign up to request clarification or add additional context in comments.

3 Comments

hi, where am i suppose to write the code for the serializeGridData? and what is that hello for?
i've observe that when i copy paste the code for serializeGridData in function jqFunctions() { this.serializeGridData = function(postData) {...} it isn't working... and also, i don't get the purpose what is that new jqFunctions().serializeGridData("hello"); for
if you put this code between <script></script> or in a separate .js file, it'll work. jqFunctions is "class". new jqFunctions() creates an object. "hello" is the parameter i'm passing to serializeGridData. It gets assigned to "postData" in function(postData). inside the function, when you call alert(postData), it does alert("hello")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.