2

I create a component which is calling a search event which has a associated search bar component. Idea is get the text from search bar and dynamically filter the results returned.

Problem, I am always getting myEvent is null. from SearchBarController.js, why is that?

MyComponent:

<aura:handler event="c:SearchKeyChange" action="{!c.searchKeyChange}"/>

MyComponentController.js

    searchKeyChange: function(component, event) {
    console.log('>>>> Account Opportunity Transactions searchKeyChange() - start');        
    var searchKey = event.getParam("searchKey");
    console.log('>>>>  searchKey - ' + searchKey);        
    var action = component.get("c.findByName");
    //parameter that is passed to the apex server side
    action.setParams({
      "searchKey": searchKey
    });
    action.setCallback(this, function(a) {
        component.set("v.accounts", a.getReturnValue());
    });
    $A.enqueueAction(action);
    console.log('>>>> Account Opportunity Transactions searchKeyChange() - end');                
}    

SearchKeyChanged: (Event Component)

<aura:event type="APPLICATION" description="Search Event Component">
   <aura:attribute name="searchKey" type="String"/>
</aura:event>

There is a component SearchBar

<aura:component >
    <input type="text" class="form-control" onkeyup="{!c.searchKeyChange}"
        placeholder="Search"/>
</aura:component>

SearchBarController.js

({
searchKeyChange: function(component, event, helper) {

    try{
        console.log('>>>>>> searchKeyChange() : start');            
        var myEvent = $A.get("e.SearchKeyChange");
        if(myEvent != null){
            myEvent.setParams({"searchKey": event.target.value});
            myEvent.fire();                
        }else{
            myEvent.setParams({"searchKey": "Gene"});
            myEvent.fire();                
            console.log('>>>>>> searchKeyChange() - myEvent is null.');                                    
        }

        console.log('>>>>>> searchKeyChange() : end');                    
    }catch(e){
        console.log('>>>>>> Error searchKeyChange() : ' + e);
    }

},
})

1 Answer 1

2

believe you need a namespace when getting the event..

try changing

var myEvent = $A.get("e.SearchKeyChange");

to

var myEvent = $A.get("e.c:SearchKeyChange");

and see if it works (c is the default namespace.. if you have your own namespace, use that)

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .