Object has no method 'call' when setting Backbone Model from Trigger

本小妞迷上赌 提交于 2019-12-11 10:19:15

问题


So, I just spent about 3 hours trying to debug this nasty thing and I'm at wits end.

What I'm trying to do is fairly simple. I'm working with a search query (string) and trying to set a global model with its attributes. However, when I'm moving to a different view, I want the model to clear out these attributes, or of course when they enter a new search query, have the event fire again. I'm trying to use the Backbone Marionette Wreqr / Event Aggregator to manage the events here.

I don't know if it's because I'm using requirejs, or not scoping things correctly, but I'm hitting this very confusing error:

    Uncaught TypeError: Object #<Object> has no method 'call' backbone.amd.min.js:1
f backbone.amd.min.js:1
e.Events.trigger backbone.amd.min.js:1
i.extend.set backbone.amd.min.js:1
(anonymous function) app.js:42
f backbone.amd.min.js:1
e.Events.trigger backbone.amd.min.js:1
Application.searchRoute app.js:81
(anonymous function) backbone.amd.min.js:1
(anonymous function) backbone.amd.min.js:1
w.some.w.any underscore.amd.min.js:1
i.extend.loadUrl backbone.amd.min.js:1
i.extend.navigate backbone.amd.min.js:1
i.extend.navigate backbone.amd.min.js:1
SidebarLayout.search sidebar.js:177
(anonymous function) sidebar.js:3
x.event.dispatch jquery.min.js:5
y.handle

And here's some of my application code (my main controller). As you can see, I'm trying to set up the app.searchResult model with these other parameters from the searchRoute method.

What's so bizarre is that it works the first time the page loads, but when I attempt to use router navigate or do anything but refresh the page, it fails.

I tried using @searchResult instead, tried adding a , @ after my trigger call, and all sorts of other things. What's stranger is that inside that scope, app.searchResult is available to me. And as soon as I remove the set call from there, the error doesn't happen anymore, so I'm thinking it must be that?

define [
  'backbone.marionette',
  'global/layouts/home',
  'global/layouts/layout',
  'search/models/search-results',
  'search/collections/panels',
  'global/collections/categories',
  'global/collections/stores',
  'global/collections/searchResults',
  'search/views/panels',
  'apps/router'
], (Marionette, homeLayout, subpageLayout, SearchResultModel, PanelsCollection, CategoriesCollection, StoresCollection, SearchResultsCollection, PanelsView, Router) ->

  class Application extends Backbone.Marionette.Application

    onInitializeAfter: =>

      # Main Region
      @addRegions
        mainRegion: '#app'

      # AppRouter
      @router = new Router controller: @

      # Request Response / Vent
      @reqRes()
      @appVent()

      # Global Search Result Model
      @searchResult = new SearchResultModel()

      Backbone.history.start
        pushState:  false
        root:       ""

    searchRoute: (query, params) ->

      params = $.extend params, text: query
      @vent.trigger "search:setParams", params

      @subpageLayout = new subpageLayout()
      @mainRegion.show @subpageLayout

    appVent: ->
      @vent.on "search:setParams", (params) ->
        app.searchResult.set("params", params)
      @vent.on "sidebar:reset", ->
        app.searchResult.set("params", null)

I can send more code if you like, but the idea is to use a custom trigger like sidebar:reset to clear out these parameters. I'm also listening to some model events (all of that stuff is working great from within the context of the view and when I don't mess with the event aggregator).

Thanks in advance for your help. You have no idea how many hours I just spent on this!

来源:https://stackoverflow.com/questions/17638366/object-has-no-method-call-when-setting-backbone-model-from-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!