简体   繁体   English

未捕获错误:无法实例化多个Backbone.RelationalModel,每个类型具有相同的id

[英]Uncaught Error: Cannot instantiate more than one Backbone.RelationalModel with the same id per type

I'm using Backbone Relational in a Backbone Marionette app. 我在Backbone Marionette应用程序中使用Backbone Relational。 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. 似乎问题是该模型已经在内存中,因此Backbone Relational不想重新获取它。 It has a built in helper to help with this - findOrCreate . 它有一个内置的帮助器来帮助解决这个问题 - 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. 我遵循了backbonerails.com教程,你已经设置了一个漂亮的“when:fetched”函数,该函数基本上运行了从服务器获取模型后要运行的任何代码。

I've been trying to adjust this to instead use the findOrCreate function but thus far have been unsuccessful. 我一直试图调整这个而不是使用findOrCreate函数,但到目前为止一直没有成功。 Any ideas how I should update this to use the findOrCreate call instead of the fetch call? 有什么想法我应该如何更新它以使用findOrCreate调用而不是fetch调用?

@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. 只需使用findOrCreate实例化模型然后获取它。

var model = MyModel.findOrCreate({id : modelId});
model.fetch();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM