Inject only relative enterprises into shopfront

Other shops displayed in producer modals are lazy loaded when opening
their modal.
This commit is contained in:
Maikel Linke
2016-06-17 17:03:47 +10:00
parent db3637fe84
commit e284ad62b2
8 changed files with 61 additions and 11 deletions

View File

@@ -1,9 +1,15 @@
Darkswarm.directive "enterpriseModal", ($modal)->
Darkswarm.directive "enterpriseModal", ($modal, Enterprises, EnterpriseResource) ->
restrict: 'E'
replace: true
template: "<a ng-transclude></a>"
transclude: true
link: (scope, elem, attrs, ctrl)->
elem.on "click", (ev)=>
link: (scope, elem, attrs, ctrl) ->
elem.on "click", (ev) =>
ev.stopPropagation()
params =
id: scope.enterprise.id
EnterpriseResource.relatives params, (data) =>
Enterprises.addEnterprises data
scope.enterprise = Enterprises.enterprises_by_id[scope.enterprise.id]
Enterprises.dereferenceEnterprise scope.enterprise
scope.modalInstance = $modal.open(controller: ctrl, templateUrl: 'enterprise_modal.html', scope: scope)

View File

@@ -1,6 +1,8 @@
Darkswarm.factory 'Dereferencer', ->
new class Dereferencer
dereference: (array, data)->
dereference: (array, data) ->
if array
for object, i in array
array[i] = data[object.id]
key = undefined
key = object.id if object
array[i] = data[key]

View File

@@ -0,0 +1,8 @@
Darkswarm.factory 'EnterpriseResource', ($resource) ->
$resource('/enterprise/:id.json', {}, {
'relatives':
method: 'GET'
url: '/enterprises/:id/relatives.json'
isArray: true
cache: true
})

View File

@@ -1,4 +1,4 @@
Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer, visibleFilter, Matcher, Geo, $rootScope)->
Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer, visibleFilter, Matcher, Geo, $rootScope) ->
new class Enterprises
enterprises_by_id: {}
constructor: ->
@@ -20,14 +20,32 @@ Darkswarm.factory 'Enterprises', (enterprises, CurrentHub, Taxons, Dereferencer,
if CurrentHub.hub?.id
CurrentHub.hub = @enterprises_by_id[CurrentHub.hub.id]
for enterprise in @enterprises
Dereferencer.dereference enterprise.hubs, @enterprises_by_id
Dereferencer.dereference enterprise.producers, @enterprises_by_id
@dereferenceEnterprise enterprise
dereferenceEnterprise: (enterprise) ->
# keep a backup of enterprise ids
# in case we dereference again after adding more enterprises
if enterprise.hub_references
enterprise.hubs = enterprise.hub_references.slice()
else
enterprise.hub_references = enterprise.hubs.slice()
if enterprise.producer_references
enterprise.producers = enterprise.producer_references.slice()
else
enterprise.producer_references = enterprise.producers.slice()
Dereferencer.dereference enterprise.hubs, @enterprises_by_id
Dereferencer.dereference enterprise.producers, @enterprises_by_id
dereferenceTaxons: ->
for enterprise in @enterprises
Dereferencer.dereference enterprise.taxons, Taxons.taxons_by_id
Dereferencer.dereference enterprise.supplied_taxons, Taxons.taxons_by_id
addEnterprises: (new_enterprises) ->
return unless new_enterprises && new_enterprises.length
for enterprise in new_enterprises
@enterprises_by_id[enterprise.id] = enterprise
flagMatching: (query) ->
for enterprise in @enterprises
enterprise.matches_name_query = if query? && query.length > 0