0

Hello I have the following JSON provided from a server. With this I want to create a model with a nested model.

//json
{
    "2": {
        "sheet_id": 2,
        "distribution_id": 4,
        "areas": {
            "1": {
                "area_id": "1",
                "image_id": "164"
            },
            "2": {
                "area_id": "2",
                "image_id": "164"
            }
        }
    },
    "3": {
        "sheet_id": 3,
        "distribution_id": 4,
        "areas": {
            "1": {
                "area_id": "1",
                "image_id": "165"
            },
            "2": {
                "area_id": "2",
                "image_id": "164"
            }
        }
    },
}

I want these to be converted to two nested backbone models with the following structure:

Structure

Project has many Sheets and Sheet has many Areas
1
  • 1
    it's hardly possible to recognize what the structure you are expecting is and what you are exactly asking. Commented Mar 31, 2015 at 0:30

2 Answers 2

2

The answer largely depends on how often this sort of pattern will repeat itself.

There is fantastic backbone classes that replace or augment the Model class which lets you do what you're looking for:

Of course you can also roll a 1-off solution in parse: method to manually init models or collections of the nested resources.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Smazurov. And what do you recommend me? deep model or nested models? Did you use it with multiple nested classes?
I would probably go with Deep Model personally, but as suish mentioned in his comment, its entirely dependent on your project. The question does not have enough information to make an informed recommendation.
And what about collections? I was reading this post and may be is a native solution backbonetutorials.com/what-is-a-collection
Backbone collections are basically a way to do things with a list of models. You could represent each sheet in a Sheets collection.
0

You can also use this library,

Backbone Relational

You can create structure of models as you have mentioned using backbone relational.

Comments

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.