Adapting taxon autocompleter to allow multiple selection of taxons

This commit is contained in:
Rob Harrington
2014-12-18 15:47:20 +11:00
parent 4b353fa27b
commit a38e27e504
2 changed files with 13 additions and 10 deletions

View File

@@ -1,19 +1,22 @@
angular.module("ofn.admin").directive "ofnTaxonAutocomplete", (Taxons) ->
# Adapted from Spree's existing taxon autocompletion
require: "ngModel"
link: (scope,element,attrs,ngModel) ->
scope: true
link: (scope,element,attrs) ->
multiple = scope.$eval attrs.multipleSelection
placeholder = attrs.placeholder
setTimeout ->
element.select2
placeholder: "Category"
multiple: false
placeholder: placeholder
multiple: multiple
initSelection: (element, callback) ->
callback Taxons.findByID(scope.product.category_id)
if multiple
callback Taxons.findByIDs(scope.product.category_id)
else
callback Taxons.findByID(scope.product.category_id)
query: (query) ->
query.callback { results: Taxons.findByTerm(query.term) }
formatResult: (taxon) ->
taxon.name
formatSelection: (taxon) ->
taxon.name
element.on "change", ->
scope.$apply ->
ngModel.$setViewValue element.val()
taxon.name