PDA

View Full Version : Return values with model member function



jcoenrae
18 Aug 2011, 3:47 AM
Hello,

I want to create a member function in a model and use that function in a template (which uses that model). My member function gets called but the returned value isn't shown.

This is my model with function (note that the console.logs print the correct output):


app.models.Route = Ext.regModel("app.models.Route", { fields: [
{name: "routeName", type: "string"},
{name: "fromCity", type: "string"},
{name: "toCity", type: "string"},
{name: "custom", type:"bool"}],
getTitle: function() {
if(this.get('custom')){
console.log(this.get('fromCity') + " - " + this.get('toCity'));
return this.get('fromCity') + " - " + this.get('toCity');
}else{
console.log(this.get('routeName'));
return this.get('routeName');
}
}});

This is the template where I use the methods on the object. I just see Title:, and then nothing else.

app.views.routeTemplate = new Ext.XTemplate( '<p>Title: {[this.getTitle]}</p>');

Is it even possible to do this, because I couldn't find any examples of this.

Kind regards,

Jolien

jjerome
18 Aug 2011, 5:58 AM
This is the template where I use the methods on the object. I just see Title:, and then nothing else.
Code:

app.views.routeTemplate = new Ext.XTemplate('<p>Title: {[this.getTitle]}</p>');

what is your reference to 'this'?

jcoenrae
18 Aug 2011, 6:06 AM
this is an object of the model.

For example

{fromCity: "City from",
toCity: "City to",
custom: true,
routeName: "Name"}

It is loaded in a store and then the template is updated with the object. If I call the fields (for example
{fromCity} in the template, everything works well. The
this was just a try, it was the only time that my function was actually called.

jjerome
18 Aug 2011, 6:37 AM
I know that, that wasn't my question.. When you run through the debugger and when your XTemplate is being instantiated, what is 'this' referencing?

Also try adding a () on the getTitle?

jcoenrae
19 Aug 2011, 12:26 AM
Hi,

I'm using XCode because I'm building an iOS application and I cannot set any breakpoints in the javascript code there to see what the this object is referencing to.

If I add () to the getTitle, the panel won't show anymore and the member method isn't called.

Kind regards,

Jolien

jcoenrae
19 Aug 2011, 12:32 AM
I noticed that values.getTitle gives the same result as this.getTitle, adding ()'s also results in the panel not showing up.

jjerome
19 Aug 2011, 4:30 AM
try referencing the model directly when you make the call 'getTitle'?

jcoenrae
23 Aug 2011, 5:18 AM
I couldn't find the correct way to do it, so I just added the methods to the template too and then it worked.