Fyre
Fyre

Reputation: 1180

Backbone data logic inside view

I am not sure where to perform the action of preparing the data for the template of my view. Currently I have this piece of code.

getTemplateData: function () {
        var inventoryStatus = selectedDevice.get("inventoryStatus"),
            data = {},
            statusName,
            inventoryDate;
        statusName = getConfigValue("pdp", "statusMap", inventoryStatus);
        data.message = getConfigValue("pdp", "statusMessage", statusName);
        data.className = "";
        data.dataAttribute = null;
        data.tooltipValue = null;
        data.displayError = false;



        var redirectCode = (allDevices.get("thirdPartyRedirectCode") !== null) ? allDevices.get("thirdPartyRedirectCode") : "";
        if (redirectCode) {
            if (redirectCode === 9999) {
                data.buttonDisabled = false;
                data.buttonText = "Design Yours";
            } else if (redirectCode === 9998) {
                data.buttonDisabled = true;
                data.buttonText = "Design Yours";
            }
            return false;
        }



        switch(inventoryStatus) {
            case 1001: //Out of Stock
                data.buttonDisabled = true;
                data.displayError = true;
                break;
            case 1002: //Pre Order
                data.buttonDisabled = false;
                break;

        }

        return data;
    }

This getTemplateData() I call inside my render function of the view. This seems wrong by the looks of it and i am unsure where to place this code. Should I create different getters in my model or should i leave them inside my main view. Please help.

Upvotes: 1

Views: 101

Answers (1)

JNF
JNF

Reputation: 3730

From what I know the "correct" way of doing this is to put it in the model, and in the view have

getTemplateData: function () {
    return this.model.getTemplateData();
}

EDIT

In case of multiple models for a view (which shouldn't happen, without getting into your decisions at the moment) you can have a getTemplateData for each, and call them with something like extend:

getTemplateData: function () {
    var data = this.model1.getTemplateData();
    data = $.extend(data, this.model2.getTemplateData());
    return data;
}

BUT What you really should do, IMHO, is give each it's own view, where one of them is smaller and intended to be included in the other. (i.e. bigView.$el.append(smallView.el))

Upvotes: 2

Related Questions