diff --git a/app/models/SuperModel.coffee b/app/models/SuperModel.coffee index 50cb5906c..b91731f98 100644 --- a/app/models/SuperModel.coffee +++ b/app/models/SuperModel.coffee @@ -8,8 +8,8 @@ class SuperModel @mustPopulate = model model.saveBackups = @shouldSaveBackups(model) model.fetch() unless model.loaded or model.loading - model.on('sync', @modelLoaded) unless model.loaded - model.once('error', @modelErrored) unless model.loaded + model.on('sync', @modelLoaded, @) unless model.loaded + model.once('error', @modelErrored, @) unless model.loaded url = model.url() @models[url] = model unless @models[url]? @modelLoaded(model) if model.loaded @@ -18,12 +18,13 @@ class SuperModel shouldPopulate: (url) -> return true shouldSaveBackups: (model) -> return false - modelErrored: (model) => + modelErrored: (model) -> @trigger 'error' + @removeEventsFromModel(model) - modelLoaded: (model) => + modelLoaded: (model) -> schema = model.schema() - return schema.on('sync', => @modelLoaded(model)) unless schema.loaded + return schema.once('sync', => @modelLoaded(model)) unless schema.loaded refs = model.getReferencedModels(model.attributes, schema.attributes) refs = [] unless @mustPopulate is model or @shouldPopulate(model) # console.log 'Loaded', model.get('name') @@ -33,10 +34,15 @@ class SuperModel continue if @models[refURL] @models[refURL] = ref ref.fetch() - ref.on 'sync', @modelLoaded + ref.on 'sync', @modelLoaded, @ @trigger 'loaded-one', model: model @trigger 'loaded-all' if @finished() + @removeEventsFromModel(model) + + removeEventsFromModel: (model) -> + model.off 'sync', @modelLoaded, @ + model.off 'error', @modelErrored, @ getModel: (ModelClass_or_url, id) -> return @getModelByURL(ModelClass_or_url) if _.isString(ModelClass_or_url)