16

On page load I'm bootstrapping my data to my collections via this technique. That works great, but I'm running into an issue where I have sub-models which also need to be cast.

For example, I return a JSON collection called Book, and each book array contains 10-20 models themselves called Pages. If I try and access Pages directly, I get the [object][object] error as Backbone can't figure out what type of object it is.

Previously when I was using fetch, I would get around this by using collections which contained a parse action. In parse I would do something like:

Collection_Books = Backbone.Collection.extend({
    model: Model_Book,
    parse: function (response) {
        response.Pages = new Collection_Pages(response.Pages);
        return response;
    }
});

Now that I'm accessing the data directly and not using fetch, the documentation implies that I no longer have access to the parse method.

If I am not using fetch or calling a server on page load, how can I cast sub-models using the Books > Pages example?

2
  • If you're ok adjusting your constructor/initialize method a little bit, you can certainly set up child models there. A little more detail towards the end of this post: bit.ly/KKHItJ Commented Jun 17, 2012 at 5:06
  • Your this technique link is broken Commented May 12, 2016 at 21:05

2 Answers 2

57

Not sure if this is new, but it looks like you can also just set the parse property of the options parameter to your constructor to true, thus telling the constructor to use your parse method:

modelInstance = new MyModel(obj, {parse: true});

http://backbonejs.org/#Model-constructor

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

3 Comments

Note: this works on the collection constructor as well e.g. var books = new Collection_Books(json, {parse: true});
This is new behavior, Backbone didn't have this option when the question was asked. I guess we will just have to wait around until the "accept" gets switched.
This answer doesn't work for calling the Collections parse, only each of the Model's parses. You would think new Collection_Books(json, {parse: true}); would call YourCollection.prototype.parse but it does not. so this answer cannot be correct.
11

Update: This answer is now out of date, see uglymunky's answer for more up to date information.


You're right, parse is only specified to be called during fetch so it won't be called when you're building a collection from raw data.

But, since you control the raw data that you're using to bootstrap your collection, you can call your collection's parse yourself:

var c = new YourCollection(
    YourCollection.prototype.parse([
        // attribute data goes here...
    ])
);

Demo: http://jsfiddle.net/ambiguous/kdaZ3/

Your parse methods (both collection and model) shouldn't care about their calling context so calling them directly from the prototype should be fine.

1 Comment

I think this is about the third time you've bailed me out with backbone.js. I owe you a thanks yet again :)

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.