Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use "this" inside countrySource method #69

Open
Philosopher-G33k opened this issue Jul 8, 2018 · 4 comments
Open

Cannot use "this" inside countrySource method #69

Philosopher-G33k opened this issue Jul 8, 2018 · 4 comments

Comments

@Philosopher-G33k
Copy link

Philosopher-G33k commented Jul 8, 2018

Hi,

I am unable to use "this" inside countrySource method.
using this.get or calling this.somefunction gives error.

In hbs file:
{{aupac-typeahead action=(action (mut searchCriteria)) class='form-control' source=countrySource placeholder='Search for a country'}}

In component js:
countrySource : function(query, syncResults, asyncResults) { console.log(query); console.log(this.get("searchCriteria")); //syncResults(results); },

The error which i am getting is:
TypeError: this.get is not a function

Can anyone please point out if I am missing anything?
Am new to Ember and have exhausted all search possibilities.

Update: I understand that "this" in countrySource method is jquery context. Is there any way to get controller context in this method.

Regards

@Philosopher-G33k Philosopher-G33k changed the title Cannot use "this" inside countrtySource method Cannot use "this" inside countrySource method Jul 8, 2018
@jackmatt2
Copy link
Member

"this" is probably bound to a different context than the ember object. It is a javascript issue rather than an ember one. You can fix it in a number of ways including binding the function to a specific context.bind(), storing self somewhere const self = this and other methods.

@veelci
Copy link

veelci commented Aug 16, 2018

@Philosopher-G33k, does this work for you?

countrySource: computed(function() {
  return (query, syncResults, asyncResults) => { 
    console.log(query); 
    console.log(this.get("searchCriteria")); //syncResults(results); 
  };
}),

@csprocket777
Copy link

I'm struggling with the same thing right now. @veelci, that solution won't work because it's being called from the component and is provided from the template without an opportunity to provide a bound context. Doing the lamda function there only provides the function with the context of the component.

@veelci
Copy link

veelci commented Aug 3, 2019

@csprocket777 What version of Ember are you using?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants