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.

jayAnn
  • 827
  • 3
  • 18
  • 38
  • I don't follow -- the code you posted will work fine, what are you asking? –  Mar 22 '11 at 07:25
  • Isn't basically the same as your previous question? http://stackoverflow.com/questions/5374977/is-it-possible-to-include-an-event-in-a-javascript-function – Felix Kling Mar 22 '11 at 07: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 – jayAnn Mar 22 '11 at 08: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 – jayAnn Mar 22 '11 at 08:30
  • @jayAnn: For many people it is difficult to understand you because you use wrong terminology like "create an object function" or "create an object from the functions". One sees that you miss many basic knowledge how JavaScript define objects. I suggest to make things more easy. If you have jqGrid which work you can post more full code in your question and we show you how one can separate the parts with the `serializeGridData` for example in another js-file. OK? – Oleg Mar 22 '11 at 09:32
  • ok oleg, sorry. i am really new into this.. – jayAnn Mar 22 '11 at 09:56
  • @jayAnn: Not a problem. I just try to find the way how one can help you. – Oleg Mar 22 '11 at 10:02
  • Could you additionally explain why you want place all parameters send to the server as the properties of another parameter `json`? Which technology you use on the server side (ASP.NET MVC, ASP.NET WCF, ASMX web service, PHP, Java servlet or some other)? How look like the server method definition which you use? In the posted code you has just `url: ''` and it gives no information. – Oleg Mar 22 '11 at 10:07
  • 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? – Oleg Mar 22 '11 at 10:23
  • ok oleg...the code you gave me in the first one works now.. thanks a lot..and thank you for bearing me Oleg:) – jayAnn Mar 22 '11 at 12:43

1 Answers1

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");
Abdo
  • 13,549
  • 10
  • 79
  • 98
  • hi, where am i suppose to write the code for the serializeGridData? and what is that hello for? – jayAnn Mar 22 '11 at 08:37
  • 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 – jayAnn Mar 22 '11 at 09:15
  • 1
    if you put this code between 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") – Abdo Mar 22 '11 at 10:08