Value not bound to Ko.observable in knockout.js - javascript

This is the code I use to bind to a textbox:
var CategoryViewModel = {
categoryModel: ko.observable({
categoryId: ko.observable(),
categoryName: ko.observable(),
active: ko.observable()
}),
GetCategoryById: function (data, event) {
CategoryViewModel.ClickId(event.target.id);
var ajaxUrl = ApplicationRootUrl("GetCategoryById", "Category") + "/" + CategoryViewModel.ClickId();
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
url: ajaxUrl,
dataType: "json",
success: function (data) {
if (data.isSuccess) {
// This value got bind to textbox
CategoryViewModel.categoryModel(data.data);
}
},
error: function (err) {
}
});
},
CategoryAddButton: function() {
CategoryViewModel.categoryModel();
$('#addCategoryModel').modal('toggle');
}
};
$(document).ready(function () {
ko.applyBindings(CategoryViewModel, document.getElementById("categoryMain"));
});
The CategoryAddButton method is called on button click. I am trying to empty the model value in this method.
Here is the HTML:
<input type="text" name="CategoryName" class="form-control" placeholder="Enter Category Name" data-bind="textinput: categoryModel().categoryName">
The textbox value gets bound on ajax call. However, after the CategoryAddButton method is called, the value does not get bound to the textbox.

First, I'd advise you using a different approach to creating viewmodels from what you've written. Even though some beginner's examples do the same, that's only for simplicity - in reality it is often not a good idea to create viewmodels as object literals. This is because as posted here, here, and here among many other duplicates, accessing another property of the same object can get pretty dirty despite how trivial that task should be.
So, to get over this problem, you should use constructors and the new operator instead, because that enables you to manipulate the object significantly easier. However, I've added this only as a guide to you to write cleaner code, using constructors and new object syntax won't solve the problem alone.
So let's get back to your question. To find out the reason why your code does not work, look how you manipulate your data when the binding works and how when it doesn't.
You said that after the AJAX-call had succeeded the value got updated properly so the binding worked. This is because in the success callback of the AJAX-call, you are actually passing an object into categoryModel. However, I'd point out that what you pass to it is not an observable but just a plain object, whereas you initially create it with its properties being observables! So even there you might run into problems.
You also said that after the button had been clicked, the value was not updated. I am not really sure what you even want to achieve here; what do you want to be displayed and where should the data come from? Because this line of code you've written:
CategoryViewModel.categoryModel();
is simply a getter -- it won't alter the object in any way, you are just reading its value. Without actually modifying it, of course nothing will change.
So, I'll give you a possible way of implementing the whole thing, and I suggest you read more on javascript object constructors and how to use knockout properly using them.
function categoryViewModel() {
var self = this;
// Don't wrap these in another observable, that's totally needless.
this.categoryId = ko.observable(null);
this.categoryName = ko.observable(null);
this.active = ko.observable(true);
// You could actually define this on the prototype
// but I don't want to make it even more complicated
this.GetCategoryById = function(categoryId) {
// You could do this if the value passed can either be a plain value
// or an observable; ko will return the actual value to you.
var catId = ko.utils.unwrapObservable(categoryId);
var ajaxUrl = ApplicationRootUrl("GetCategoryById", "Category") + "/" + catId;
$.ajax({
type: 'GET',
contentType: "application/json; charset=utf-8",
url: ajaxUrl,
dataType: "json",
success: function(data) {
if (data.isSuccess) {
// Correct the object references according to
// the structure of the response if needed.
self.categoryId(data.data.id);
self.categoryName(data.data.name);
self.active(data.data.isActive);
}
},
error: function(err) {
}
});
};
this.CategoryAddButton = function() {
self.categoryId(null);
self.categoryName(null);
self.isActive(true); // If you want to default to true.
$('#addCategoryModel').modal('toggle');
};
};
$(document).ready(function () {
ko.applyBindings(new categoryViewModel(), document.getElementById("categoryMain"));
});
And your HTML could be:
<input type="text" name="CategoryName" class="form-control" data-bind="textInput: categoryName" />
As for the GetCategoryById function, it would even be nicer if you assigned that to the function prototype, rather than assigning a "copy" to each and every object created. But since I assume you'll only ever have 1 instance of your viewmodel, I'll consider that out of scope for now.

Related

Ajax response is not changing my viewmodel

I bind a value and a list and want to change it from inside an Ajax callback. I retrieve a fresh value in .get(), but when I the callback for .get() actually happens, and I assign the retrieved value to my view model's property, the UI does not refresh. Here is my code:
function SearchViewModel() {
this.count = ko.observable(count);
this.list = ko.observableArray(list);
//I had count and list before I assigned.
this.addPage = function() {
var form = $('#form');
var serializedData = form.serialize();
$.get("{% url 'search:search' %}", serializedData, function(response){
console.log(this.count); // it's undefined here.
this.count = response.count;
console.log(this.count); // it's the value I want to updated, e.g. 20. But UI is not refreshedenter code here
});
};
}
I want to update a list too in the callback, but right now even a simple count value is not being updated. I read about many related solutions on stackoverflow, and tried several, but none of them worked.
Try:
this.count(response.count);
That should do the trick.
For more information about observables check out http://knockoutjs.com/documentation/observables.html
You also have a potential scope issue in your code. When you are reffering to this in the callback you are not guaranteed that you get the scope of the viewModel. Therefore you should add this line outside of the callback:
var self = this;
And inside the callback you should change to:
self.count(response.count);

Enable binding breaks when observable array updated via Ajax success callback

http://jsfiddle.net/FZ6K6/24/
I have a button (Remove inputs) with enable and css bindings that are returned when an observable array contains more than 2 items.
<button data-bind="click: removeInput, enable: Integers().length >2, css { red: Integers().length >2 }">Remove Input</button>
I also have a function (loadIntegerSorter) that sets the observable array to contain 2 items.
self.loadIntegerSorter = function () {
self.Integers([new integer(0, 0, 0), new integer(0, 0, 0)]);
};
I also have a save function that submits via ajax. Within the success callback, loadIntegerSorter is called.
success: function (result) {
if (result.Status == "success") {
isvm.loadSortedIntegers();
}
}
However, this seems to break the enable binding. The CSS binding behaves as expected with the array items = 2. But the Enable binding does not. I can run loadIntegerSorter outside of the Ajax function successfully so I suppose this is a synchronization problem but I don't know what the solution is.
The fiddle I've linked to doesn't fully demonstrate the problem because it depends on making a genuine Ajax request. But I hope it shows enough to understand.
Elaboration:
This results in the expected behaviour from the enable binding:
self.save = function () {
self.isloading();
};
But this doesn't:
self.save = function () {
$.ajax("/Home/Index", {
data: ko.toJSON(self.integerSorter),
cache: false,
type: "post",
contentType: "application/json",
context: self,
success: function (result) {
this.isloading();
}
});
};
And nor does this:
self.save = function () {
self.isloading();
$.ajax("/Home/Index", {
data: ko.toJSON(self.integerSorter),
cache: false,
type: "post",
contentType: "application/json",
context: self,
success: function (result) {
}
});
};
Whatever the cause of the problem, it seems to be related to the ajax call.
1)
Inside of your self.save function you're calling
self.isLoading(true);
Which yields
TypeError: 'undefined' is not a function (evaluating
'self.isLoading(true)')
telling you that self.isLoading is not declared anywhere in your code. This will break code execution even before the ajax request is sent.
2)
Same as 1) but this time for self.msgbox.status(). Undeclared: will break your code.
3)
The function self.loadIntegerSorter appears as self.loadSortedIntegers in the success function. Also, the self.save function appears declared two times. The second one will ovverride the first, but I guess the first one is there just in the fiddle.
4)
Inside of the success function, result.Status doesn't have any sense. You must understand that result is just a string of plain text, accessing the Status property of a string will result in an error. Perhaps you expect the response to be a JSON object with a Status property? If that is the case, you have to deserialize the string either by yourself (JSON.parse(response)) or by telling jQuery to do that for you (replace $.ajax with $.getJSON).
However, it may also be that you're not receiving any JSON back and you just wanted to access the response status, assuming you could do it that way. You can't. Being inside of a success function, you already know that your request has been successfully sent and a response received. No need to check it again.
5)
You're calling the loadSortedIntegers() method on the variable isvm. That's a totally wrong approach, even if it should work now it may cause huge troubles in the future. isvm is a global variable you use to contain an instance of your viewModel. The success function is contained in the viewModel itself, you should access it's own methods with this or self. A class should not access an instance of itself with a global variable. Question: how can I make this and/or self available in the success function? this can be reached by setting the context property to your $.ajax object. Exactly as you write success: function(){} you should write, just before that, context: this or, in your case, context: self.
Do that, and then just change the success function contents with this.loadSortedIntegers().
I've took the liberty to make some edits to your fiddle. Take your time to examine the difference here and to run it here.
Try to use valueHasMutated to push update for observable directly:
self.loadIntegerSorter = function () {
self.Integers([new integer(0, 0, 0), new integer(0, 0, 0)]);
self.Integers.valueHasMutated();
};

Variables in js

I am quite a noob in regards to js and today I come across bit of js I don't really understand and I would like to. Could you please shed some light ? There is Kendo involved but the question is plain js.
I have a nested grid, eg. each row can expand into other grid and each of this grid has its own datasource. I populate the datasources via the method below one by one as user clicks and I had a problem with forcing the datasource to read when it receives async response from the create call. (calling the read is pretty much incorrect thing to do, but Kendo has its own bugs - not a point here.). My problem was, I didn't have an instance to call the read() on, the method only returns datasource and assigns it to a grid, when the event comes back I can't find any reference to anything I could get the correct datasource instance from. this is different context in here.
In order to resolve this I added a datasource variable into the method what builds the datasource and I return the variable instead the datasource, which is the same thing. However this helps to have something to call the problematic read() on. Now in my create handler I call create on the variable I am returning in the method during innit. Well it works, but I am not sure if every datasource is calling read on its own instance after innit ?
function _getDatasource() {
var datasource = new kendo.data.DataSource({
transport: {
read: {
url: serviceBaseUrl + "ReadQuestionnaire",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
},
create: {
url: serviceBaseUrl + "CreateQuestionnaire",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
complete: function (jqXhr, textStatus) {
if (CheckForExceptions(jqXhr, textStatus) == false) {
// this is the variable I am not sure about
// after innit does this always refers to this same datasource ?
datasource.read();
}
}
}
}
});
return datasource;
}
Your solution is correct, and yes, the datasource.read() call is the correct datasource object in each case.
Here's why this works: closures.
A closure is the ability to have a variable declared in one function accessible from a nested function. Or, for a more accurate description, see the wikipedia page: http://en.wikipedia.org/wiki/Closure_(computer_science)
Here's a very simple example:
function doStuff(){
var name = "Derick";
function sayMyName(){
console.log(name);
}
sayMyName();
}
doStuff();
In this example, I'm declaring a variable name inside of the doStuff function. Then I'm nesting another function inside of the first function. The sayMyName function is accessing the name variable through the use of a closure.
When I call doStuff(), the variable is defined and assigned to a value. Then the sayMyName function is defined and it uses that variable. I then call sayMyName() and it logs the name to the console.
Similarly, in your code you are creating a variable that is assigned to the instance of the DataSource. Later, you are defining a function for the complete callback. After the data source has loaded and the complete callback is fired, you are accessing the same dataSource variable that you had assigned to the DataSource instance through the use of a closure around that variable.
Since you are declaring var dataSource every time you call _getDataSource, you are creating a new variable / reference, assigned to a new DataSource instance. I don't think you need to return datasource at the bottom of your function, though... at least, not for the complete callback function to work. Maybe you need it for something else, outside of this function, though?
For more information on closures in JavaScript:
https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Closures
How do JavaScript closures work?
http://www.javascriptkit.com/javatutors/closures.shtml
http://www.watchmecode.net/javascript-scope (paid screencast)
HTH

Proper name for a method that sets a property but also sends a request

Hopefully, this won't be a silly question.
Assume I have the following model:
var Appointment = Backbone.Model.extend({
urlRoot: '/appointments',
defaults: function() {
return {
'Date': new Date(),
'Status': AppointmentStatus.UPCOMING
};
}
});
I want to add a method to Appointment that is purely for updating the Status. This method would do the following:
function(newStatus) {
$.ajax(this.url() + '/status', {
data: { status: newStatus },
type: 'POST',
success: _.bind(function () {
this.set('Status', newStatus);
}, this);
});
}
The difficult part is determining a semantically correct name for this method. Should it be setStatus, updateStatus, or something else? I'd like the name to be easily differentiated from a regular set (which is why setStatus seems wrong).
I agree with you, "updateStatus" makes the most sense to me - it more accurately communicates that there is more involved in its operation than just setting a variable somewhere.
You said the answer yourself "...purely for updating the Status." It's a silly question but I think you should go ahead with updateStatus.
updateStatus more accurately portrays the method than setStatus, especially since it's making an ajax request and all. I would reserve names like setStatus for when you're initializing something.

JS Prototype scope with this/bind

I have the following code that creates an object in JavaScript. It uses prototype to define functions and constructors.
function objectClass(){
this.variables = new Array();
}
objectClass.prototype =
{
contructor: objectClass,
setInfo: function(){
$.ajax({
url: "info.json",
success: function(){
//for each json element returned...
this.variables.push(json[i]);
}
});
}
getInfo: function(){
return this.variables;
},
}
This is a similar example of what I am trying to do. I need to be able to return the array of variables when I call obj.getInfo(). It always throws an error. I believe it is because the "this" is referring to the scope of the ajax success function.
Any ideas on how to get it to reference the objects variable?
That's correct, the this value is not automatically passed and thus not set to the instance. To force this, you can use the context property that $.ajax accepts:
$.ajax({
context: this, // `this` is the instance here
This sets the this value inside the success callback to the one you specified.

Categories

Resources