Last active
          August 29, 2015 14:00 
        
      - 
      
- 
        Save lucasrenan/11218109 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | var App = App || {}; | |
| App.ac_appender = { | |
| /* | |
| * ======================================== | |
| * | |
| * requires jquery autocomplete | |
| * | |
| * ======================================== | |
| * @ | |
| * ======================================== | |
| */ | |
| ac_appenders: [], | |
| /* | |
| * ======================================== | |
| * + | |
| * ======================================== | |
| */ | |
| create: function (options) { | |
| this.ac_appenders.push( new this._Autocomplete(options) ); | |
| return this.ac_appenders[this.ac_appenders.length-1]; | |
| }, | |
| delete: function (object) { | |
| return (this.ac_appenders.splice(this.ac_appenders.indexOf(object), 1)) ? true : false; | |
| } | |
| }; | |
| /* | |
| * ======================================== | |
| * - | |
| * ======================================== | |
| */ | |
| App.ac_appender._Autocomplete = function (options) { | |
| this.el = options.field || '.autocomplete'; | |
| this.receiver = options.receiver || '.autocomplete_receiver'; | |
| this.mediator = options.mediator; | |
| this.identifier = options.identifier || $(this.receiver).find('label:first-child input').attr('id'); | |
| this.cache = {}; | |
| this.init(this); | |
| }; | |
| App.ac_appender._Autocomplete.prototype.init = function (self) { | |
| var self = self; | |
| $(self.el).autocomplete({ | |
| source: function(request, response) { | |
| console.log($(self.el).data('id')); | |
| var term = request.term, | |
| url = $(self.el).data('url'), | |
| filter = [ $(self.el).data('id') ]; | |
| if (term in self.cache) { | |
| response(self.cache[term]); | |
| return; | |
| } | |
| $.each($('[id^='+self.identifier+']'), function (i, o) { | |
| var to_filter = $(o).attr('id').replace(self.identifier, ''); | |
| if (to_filter != '') filter.push(to_filter); | |
| }); | |
| $.getJSON(url, { term: term, filter: filter }, function(data, status, xhr) { | |
| self.cache[ term ] = data; | |
| var filtered_data = []; | |
| $.each(data, function (i, o) { | |
| o.label = o.title; | |
| }); | |
| response(data); | |
| }); | |
| }, | |
| select: function (event, ui) { | |
| var newItem = $($(self.receiver).children('label')[0]).clone(); | |
| newItem.removeClass('is-hidden').children('input').attr({ value: ui.item.id, id: self.identifier + ui.item.id }); | |
| newItem.append(ui.item.title); | |
| $(self.receiver).append(newItem); | |
| $(self.el).val(''); | |
| return false; | |
| } | |
| }); | |
| }; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | App.semaphores = App.semaphores || {}; | |
| App.semaphores.new = function () { | |
| App.ac_appender.create({ receiver: '#element' }); | |
| }; | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment