In all of the examples I've seen, view model properties are accessed using the "this" keyword. For example:
This is not always possible. Consider the following example:
I have a ListView (mobile, btw) and above it I have a search text box that does an "instant search" of sorts while the user types. The text box looks something like this:
searchForItem makes an async ajax call and the success callback updates the ListView. The trouble is that "this" does not refer to the view model in the call back. What is the right way to access the view model and it's properties in this type of situation? The only way I seem to be able to do this is to make all the view models available from the global name space. For example:
This seems to work but is there a better or more elegant approach? Not to mention that I am using require.js to organize my project and would prefer to have nothing in the global namespace.
BTW, I am running into the same problem with declarative binding. Since I am using require.js it seems like the only way I can reference tthe view model is something like this:
Thanks!
this
.get(
"name"
)
I have a ListView (mobile, btw) and above it I have a search text box that does an "instant search" of sorts while the user types. The text box looks something like this:
<
input
type
=
"text"
data-value-update
=
"keyup"
data-bind
=
"value: searchText, events: { keyup: searchForItem }"
/>
//app is global
app.viewmodels.carsViewModel.get(
"cars"
).push(car);
BTW, I am running into the same problem with declarative binding. Since I am using require.js it seems like the only way I can reference tthe view model is something like this:
<
div
id
=
"carsView"
data-role
=
"view"
data-model
=
"app.viewmodels.carsViewModel"
>
Thanks!