I'm using Backbone Relational in a Backbone Marionette app. If I follow this workflow: Navigate directly to the show page > click "Home" (or index page) link > click the browser's back button to go back to the show page I get an error which causes the page not to load:
Uncaught Error: Cannot instantiate more than one Backbone.RelationalModel with the same id per type!
It seems the issue is that the model is already in memory so Backbone Relational doesn't want to re-fetch it. It has a built in helper to help with this - findOrCreate. I followed the backbonerails.com tutorials which has you setup a nifty "when:fetched" function that basically runs whatever code you want to run after the model has been fetched from the server.
I've been trying to adjust this to instead use the findOrCreate
function but thus far have been unsuccessful. Any ideas how I should update this to use the findOrCreate
call instead of the fetch
call?
@TheoremReach.module "Utilities", (Utilities, App, Backbone, Marionette, $, _) ->
App.commands.setHandler "when:fetched", (entities, callback) ->
xhrs = _.chain([entities]).flatten().pluck("_fetch").value()
$.when(xhrs...).done ->
callback()
Alternatively, is there a way to access the "model in memory" that already exists and is throwing this error? I've been searching but haven't been able to figure out where that's stored/how to access that.
Thanks in advance!
Just use findOrCreate
to instantiate the model and then fetch it.
var model = MyModel.findOrCreate({id : modelId});
model.fetch();