These are chat archives for canjs/canjs

21st
Jul 2016
Sunil George
@georgesunil81
Jul 21 2016 18:42
@phillipskevin , I am facing a strange issue. In my can.control, outside the init, I have a function called refreshTasks that I call from init as this.refreshTasks() and it works fine. But, when I call this.refreshTasks() from a function called from my stache (through a can-click on a button), it does not find it! It says cannot call refreshTasks of undefined!
Not sure what I am doing wrong, or how to make it work.
Do I need to do super or something?
Kevin Phillips
@phillipskevin
Jul 21 2016 18:44
how do you call it in your template?
Sunil George
@georgesunil81
Jul 21 2016 18:44
What I am trying to do is to have a common function called refreshTasks() that I can call from init and also from a function I call on click of a refresh button that I provide on the view.
<button type="button" id="refresh-button" class="btn btn-primary btn-sm btn-block" ($click)='getTasks(id)'>Refresh</button>
I have put getTasks on scope so that the ($click) is able to find it.
But when I call this.refreshTasks() from the getTasks function, it is not able to find the refreshTasks method!
I cannot call the refreshTasks() directly instead of getTasks() from the template. I need to call refreshTasks from getTasks.
Sunil George
@georgesunil81
Jul 21 2016 18:51
Its like it is losing the reference or something.
Kevin Phillips
@phillipskevin
Jul 21 2016 18:52
you’re not calling it with a reference
when you pass it to the view, try doing something like
{
    getTasks: this.getTasks.bind(this)
}
Sunil George
@georgesunil81
Jul 21 2016 18:53
Oh! Let me try that.
That worked for me buddy. I am trying to understand, why I have to do that though?
Is it that when stache is called from can.control, the context of 'this' changes?
Kevin Phillips
@phillipskevin
Jul 21 2016 18:59
when you pass a scope to the template, you’re just giving it a raw object
so it’s calling getTasks(), not theControl.getTasks()
so this inside of getTasks is the window, not the instance of your Control
Sunil George
@georgesunil81
Jul 21 2016 19:01
Hmmm..makes complete sense to me now. Thanks @phillipskevin a lot!
Kevin Phillips
@phillipskevin
Jul 21 2016 19:01
the other thing you can do is just pass the control to the template
Sunil George
@georgesunil81
Jul 21 2016 19:01
okay
How do I do that?
Kevin Phillips
@phillipskevin
Jul 21 2016 19:02
I’m not entirely sure how your code is set up, but it should be possible to just pass this as the second parameter of can.view
Sunil George
@georgesunil81
Jul 21 2016 19:02
Currently, this is what I do in my init of the can.control -
var scope = this.scope = new(can.Map.extend({
getTasks: this.getTasks.bind(this),
}))();
I then call el.html(can.view("app/pages/job/TaskView", scope));
In such a setup, how would I pass 'this' to the can.view? I need the scope too.
Kevin Phillips
@phillipskevin
Jul 21 2016 19:04
you can’t I guess
did you try just passing this.scope
instead of scope
Sunil George
@georgesunil81
Jul 21 2016 19:05
No, haven't tried that yet.
Will try. I will let you know what I find.