Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I want to refactor this code into js object, but not sure how. How should I do it?

I initialize an object for every item in the DS.data_per_folder array. how can I define it in a higher level? Where should I init it then?

   .. 
    DS.data_per_folder[CurrentContextMgr.current.folder.attr('id')] = {
                allAttachments_vector: [],
                allAttachments_vector_counter : 0,
                attachment_position_dict : {},
                extraDetails_dict: {},
                getAttachmentsPerPage: function (pageNum) {
                    var result = this.allAttachments_vector.slice(global_vars.NUM_OF_ATTACH_PER_PAGE * (pageNum - 1), global_vars.NUM_OF_ATTACH_PER_PAGE * (pageNum));
                    return result.length == 0 ? null : result;
                },

                getAttachmentsMarkupPerPage: function (pageNum) {
                    var attachmentsMarkup = "";
                    var attachments = this.getAttachmentsPerPage(pageNum);
                    for (key in attachments) {
                        attachmentsMarkup += attachments[key].attachment_markup + " ";
                    }
                    return (attachmentsMarkup == "") ? "No attachments to display" : attachmentsMarkup;
                },

                pushAttachment: function (attachment, position) {

                    //not in dict yet
                    if (this.attachment_position_dict[attachment.attachmentId] == undefined)
                    {
                    if (position ==  null)
                    {
                    position = this.allAttachments_vector_counter;
                    }

                    this.allAttachments_vector[position] = attachment;

                this.allAttachments_vector_counter++;
                this.attachment_position_dict[attachment.attachmentId] = position;
                //this.extraDetails_dict[attachment.attachmentId] = {};
                }
            },       };
    }
..
share|improve this question
    
I initialize an object for every item in the DS.data_per_folder array. how can I define it in a higher level? Where should I init it then? – Elad Benda Feb 24 '12 at 22:59

1 Answer 1

up vote 1 down vote accepted

I would recommend using a constructor function to create the objects:

function FolderData() {

    this.allAttachments_vector_counter = 0;
    this.allAttachments_vector = [];
    this.attachment_position_dict = {};
    this.extraDetails_dict = {};

}

FolderData.prototype = {

    constructor: FolderData,

    getAttachmentsPerPage: function (pageNum) {
        var result = this.allAttachments_vector.slice(
            global_vars.NUM_OF_ATTACH_PER_PAGE * (pageNum - 1), 
            global_vars.NUM_OF_ATTACH_PER_PAGE * (pageNum)
        );
        return result.length == 0 ? null : result;
    },

    getAttachmentsMarkupPerPage: function (pageNum) {
        var attachmentsMarkup = "";
        var attachments = this.getAttachmentsPerPage(pageNum);
        for (key in attachments) {
            attachmentsMarkup += attachments[key].attachment_markup + " ";
        }
        return (attachmentsMarkup == "") ? "No attachments to display" : attachmentsMarkup;
    },

    pushAttachment: function (attachment, position) {
        //not in dict yet
        if (this.attachment_position_dict[attachment.attachmentId] == undefined) {
            if (position ==  null) {
              position = this.allAttachments_vector_counter;
            }
            this.allAttachments_vector[position] = attachment;
            this.allAttachments_vector_counter++;
            this.attachment_position_dict[attachment.attachmentId] = position;
            //this.extraDetails_dict[attachment.attachmentId] = {};
        }   
    }

};

...

DS.data_per_folder[CurrentContextMgr.current.folder.attr('id')] = new FolderData();

The benefit of this, other than readability, is that you don't create a separate copy of every function property for each object. The functions are instead attached to the constructor's prototype, so there is only one instance of each function, and functions are shared by each constructed object instance.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.