0

나는 이것이 가장 쉽고 빠르다고 느낀다. 그러나 적어도 나에게는 해결책이 도출하기 어렵다.확장 된 jquery 위젯 자동 완성의 프록시

기본적으로 일부 기능을 포함하도록 jQuery 자동 완성 위젯을 확장하려고합니다. select 이벤트에서 this.options.primaryField를 참조해야합니다. 여기

$.widget("mynamespace.myAutoComplete", $.ui.autocomplete, { 
    options: { 
     // our options 
     selectedId:0, 
     searchString:'', 
     indexField:'', 
     primaryField:'', 
     secondaryField:'', 

     // set existing ones 
     autoFocus: true, 
     minLength:3, 

     select: function(event,ui) { 
      // runs, but cant access this.options.<anything> 
     } 

    }, 

    _myselect: function (event,ui) { 
     //does not run 
     console.log("in myselect"); 
    }, 

    _renderItem: function (ul, item) { 
     var icon='marker'; 
     return $("<li>") 
      .append ('<div><a><i class=\"fi-' + icon + '"></i>' + item[this.options.primaryField] + "<br>" + item[this.options.secondaryField] + "</a><div>") 
      .appendTo (ul); 
    } 
}); 

내가 찾은 가능한 해결책 : Howto access data held in widget options when inside a widget event handler, jQuery Widget Factory access options in a callback methodjQuery-ui: How do I access options from inside private functions

하지만 그들 중 하나가 동작하지 않습니다. 나는 단지 (자연적으로 $의 된 .widget에 추가 (... 클래스)

_create: function() { 

     // does not fire _myselect on select 
     this._on(this.select, { change: "_myselect"}); 
     this._on(this.select, $.proxy(this._myselect, this)); 
     this._on(this.select, $.bind(this._myselect, this)); 

     this._super(); 
    }, 

나는 또한뿐만 아니라 _init 함수들을 추가하는 시도했습니다. 이것은 내가 그 솔루션에서 파생 한 코드 항목을 클릭하면 this.options.selectedId,되는 searchString 등을 설정할 수 있도록하려면 사전에

감사

답변

0

마지막으로 다른 사람이 같은 문제 타격은이 못을 박았다 :.

$.widget("ui.autocomplete", $.ui.autocomplete, { 
_init: function() { 
    this.element.on("autocompleteselect", $.proxy(this.myselect, this)); 
}, 
options: { 
    // our options 
    selectedId:0, 
    searchString:'', 
    indexField:'', 
    primaryField:'', 
    secondaryField:'', 
    .... 
}, 
myselect: function (event, ui) { 
    // event and ui work as normal 
    // this, refers to the widget, so the options are available via 
    // this.options.<blah> 
} 
합니다.

나는 또한 질문에서 선택한 이름 공간이 문제에 영향을 미쳤다는 것을 지적해야합니다. ui.autocomplete 이외의 다른 것을 사용하면 (이 코드가있는 곳에서는 수정되지 않은 자동 완성을 액세스 할 수 없게됩니다) 이벤트가 실행되지 않습니다. 즉, ui.autocomplete라는 네임 스페이스를 사용하더라도이 질문에서 언급 한 세 가지 this.on .. 명령은 여전히 ​​실패했습니다.