This is a migrated thread and some comments may be shown as answers.

Using “this” inside the Viewmodel?

4 Answers 51 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
la
Top achievements
Rank 1
la asked on 13 Jan 2014, 01:09 PM
I have this HTML:

<div id="bills" data-role="view" data-model="Bills" data-init="Bills.init" data-layout="default">
<h1 data-bind="text:name">Text</h1>
</div>


and the ViewModel for this is:

var Bills = kendo.observable({
  name: "John Doe",
  init: function() {
  this.doSomething();
},

doSomething: function() {
  alert(this.name);
}
});


Both codeparts with "this" are not working. "this.doSomething" produces the error:Object [object Object] has no method 'doSomething'
Why is it not possible to use "this" in such a ViewModel?

4 Answers, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 13 Jan 2014, 02:06 PM
Hi La mum,

View events cannot be bound via MVVM bindings (e.g. with data-bind attribute) which is why they are set directly as an object fields. For example:
<div id="bills" data-role="view" data-model="Bills" data-init="Bills.init" data-layout="default">

This is correct, however in this case the init method will not be executed in the context of the ViewModel but in the context of the View. This means that the 'this' keyword will refer to the View which does not have doSomething method.

Regards,
Alexander Valchev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
la
Top achievements
Rank 1
answered on 13 Jan 2014, 02:13 PM
Thank you. Is there another way to to "connect" the observeable with the view? I mean i would like to encapsulate all of my properties and my functions in one obejct und a view should use this object as the model. So every view has its own viewmodel. Does a best practice for this somewhere exist ?
0
Alexander Valchev
Telerik team
answered on 13 Jan 2014, 02:30 PM
Hello,

Since the View events cannot be bound directly, you can reference the ViewModel as a variable. For example:
var Bills = kendo.observable({
  name: "John Doe",
  init: function() {
    Bills.doSomething();
  },
  doSomething: function() {
    alert(this.name);
  }
});


Regards,
Alexander Valchev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Alexander Valchev
Telerik team
answered on 13 Jan 2014, 02:30 PM
Hello,

Since the View events cannot be bound directly, you can reference the ViewModel as a variable. For example:
var Bills = kendo.observable({
  name: "John Doe",
  init: function() {
    Bills.doSomething();
  },
  doSomething: function() {
    alert(this.name);
  }
});


Regards,
Alexander Valchev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
General Discussions
Asked by
la
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
la
Top achievements
Rank 1
Share this question
or