2

I have a situation where I need to save a couple of objects for a single person, but one object at a time, and when fetched, I get all the objects as an array along with the person's info.

This is how the model looks:

//Model for one save
{
    personID: 1,
    personName: 'John Doe',
    score: [
        {
            subject: 'English',
            marks: 30
        }
    ]
}

Fetch returns the collection like this, and I only need to render the marks on the UI as a list:

//Collection of marks for a person
{
    personID:1,
    personName: 'John Doe',
    score: [
        {
            subject: 'English',
            marks: 30
        },
        {
            subject: 'Maths',
            marks: 30
        },
        {
            subject: 'Geography',
            marks: 30
        }
    ]
}

The problem with this structure is that I cannot insert the model in the already rendered collection, since the structure is different hence the UI will not update.

How do I structure it in a better way? Or, can I treat the marks object as a model, and do the save/update using only the collection?

3
  • Can you clarify what you mean by " ... I cannot insert the model in the already-rendered collection ...."? Is the collection that is already rendered a collection of Users or a collection of Score(s)? Commented Oct 7, 2016 at 13:56
  • Meaning, when the Person page loads, I fetch the Score collection by the person ID. Then render it by passing it to another view for rendering the marks list. I can add new marks for this person from that list view. The idea is to save the new marks model for this person and also add it to the existing collection, hence avoiding the need to call fetch immediately for new data. Commented Oct 7, 2016 at 14:03
  • You appear to be conflating what is a Collection in Backbone with how the data is stored inside a model. The JSON as given above is representative of the User in your system, which happens to have an array of score objects inside of it. The items in the score array don't have an ID, so attempting to treat them as a full-blown Collection will be difficult without adding that. Beyond that, keep in mind that Backbone does not (natively) support nesting a Collection inside a Model. Can you provide some code showing the User model? Commented Oct 7, 2016 at 14:48

1 Answer 1

1

By storing multiple level JSON in a Backbone model, you lose the ability to track events around model changes.

I'd suggest you create a scores Collection and score Model which will fire events the UI can respond to.

If you need to link the person Model to the scores Collection at the data level, a good extension to Backbone to deal with this is: http://backbonerelational.org/

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

3 Comments

I already have both the Scores collection and the model! Is it possible to push the newly created model to the collection and then save it via the collection to the server? (I'm just getting comfortable with Backbone so inexperienced)
Thats a big question :) yes, that's all possible. You'll find a lot of answers in the Backbone docs, but to start you off, you'll want to call model.save(...), you can setup the API URL on either the model or collection (docs), if the model is added to a collection with URL configured, then save will by default trigger a POST, like the docs mention. If you want to trigger a save of all new models from the collection, you'll have to write that function, but model.isNew() exists, so it wouldn't be hard.
Thanks :) The docs are a little confusing! Currently I am working with model.save which saves my data to the backend. But, now I want to update the existing collection without making an additional call to Collection.fetch.

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.