copenndthagen
copenndthagen

Reputation: 50732

Ember Utility class for accessing data across different places

I am new to Ember and want a Utility class in Ember which does the following, takes in rowItems and returns an object (finalMeta)

var myMeta1 = new Array();
var myMeta2 = new Array();

dojo.forEach(rowItems, dojo.hitch(this, function(rowItem){

    var metaData = {
            Id: rowItem.Id,
            version: rowItem.version
    };

    if(rowItem.tranMetaData.tpl){
        myMeta2.push(metaData);
    }else{
        myMeta1.push(metaData);
    }
}));

if(myMeta1.length == 0){
    myMeta1 = null;
}

if(myMeta2.length == 0){
    myMeta2 =  null;
}

var finalMeta = {
    "myMeta1": myMeta1,
    "myMeta2": myMeta2
};

return finalMeta;

Where/How do I write this Utility class, such that it can be accessed from a different place (say from a different route) ?

Just to add, I want to use the finalMeta in a child route (part of some workflow) as inputs/request params to some API.

In the child route, I would then make an AJAX call,

Ember.$.ajax({
                    url: someUrl,
                    type: "POST",
                    data: JSON.stringify({
                        'ids': idKeys,
'metaData': finalMeta
                    }),
    })

Upvotes: 2

Views: 44

Answers (1)

Greg Burghardt
Greg Burghardt

Reputation: 18783

Two solutions come to mind. The first is probably the simplest to implement. The second might technically be more object oriented, but introduces another class with a very limited purpose.

The easy way: Include this as a method in your API service object:

function SomeApiService() {
}

SomeApiService.prototype = {
    constructor: SomeApiService,

    saveSomething: function(rows) {
        var finalMeta = this.getMetaData(rows);
        var idKeys = // create array of id keys

        Ember.$.ajax({
            url: someUrl,
            type: "POST",
            data: JSON.stringify({
                'ids': idKeys,
                'metaData': finalMeta
            }),
        });
    },

    doSomethingElse: function(rows) {
        var finalMeta = this.getMetaData(rows);

        Ember.$.ajax({
            ...,
            data: JSON.stringify({
                metaData: finalMeta
            })
        });
    },

    getMetaData: function(rowItems) {
        var myMeta1 = [];
        var myMeta2 = [];

        dojo.forEach(rowItems, dojo.hitch(this, function(rowItem){

            var metaData = {
                    Id: rowItem.Id,
                    version: rowItem.version
            };

            if(rowItem.tranMetaData.tpl){
                myMeta2.push(metaData);
            }else{
                myMeta1.push(metaData);
            }
        }));

        if(myMeta1.length == 0){
            myMeta1 = null;
        }

        if(myMeta2.length == 0){
            myMeta2 =  null;
        }

        var finalMeta = {
            "myMeta1": myMeta1,
            "myMeta2": myMeta2
        };

        return finalMeta;
    }
};

Or, roll this into its own helper class and have the API service class use it:

Your API Service class becomes slimmer, but introduces a dependency. You can pass your own metaHelper in the constructor and provide a mock object for testing, but it could default to a new MetaDataHelper object.

function SomeApiService(metaHelper) {
    this.metaHelper = metaHelper || new MetaDataHelper();
}

SomeApiService.prototype = {
    constructor: SomeApiService,

    saveSomething: function(rows) {
        var finalMeta = this.metaHelper.getMetaData(rows);
        var idKeys = // create array of id keys

        Ember.$.ajax({
            url: someUrl,
            type: "POST",
            data: JSON.stringify({
                'ids': idKeys,
                'metaData': finalMeta
            }),
        });
    },

    doSomethingElse: function(rows) {
        var finalMeta = this.metaHelper.getMetaData(rows);

        Ember.$.ajax({
            ...,
            data: JSON.stringify({
                metaData: finalMeta
            })
        });
    }
};

And the MetaDataHelper class doesn't contain much at this point, however you will have separated your concerns and made the meta data helper object testable by itself. This also allows you to write other API service classes that use the MetaDataHelper object to prevent the duplication of this logic.

function MetaDataHelper() {
}

MetaDataHelper.prototype.getMetaData = function(rowItems) {
        var myMeta1 = [];
        var myMeta2 = [];

        dojo.forEach(rowItems, dojo.hitch(this, function(rowItem){

            var metaData = {
                    Id: rowItem.Id,
                    version: rowItem.version
            };

            if(rowItem.tranMetaData.tpl){
                myMeta2.push(metaData);
            }else{
                myMeta1.push(metaData);
            }
        }));

        if(myMeta1.length == 0){
            myMeta1 = null;
        }

        if(myMeta2.length == 0){
            myMeta2 =  null;
        }

        var finalMeta = {
            "myMeta1": myMeta1,
            "myMeta2": myMeta2
        };

        return finalMeta;
};

Upvotes: 1

Related Questions