I am having issues with defining a function inside my viewmodel.
I fetch json data via jquery getJSON and map this data to my viewmodel.
$.getJSON('/Company/GetCompanies', function(data) {
var viewModel = new CompanyViewModel()
viewModel.model = ko.mapping.fromJS(data)
ko.applyBindings(viewModel)
});
Below is my viewmodel. As you can see what I want to do is, returning one of the properties of viewmodel via function called panyName
var CompanyViewModel = function() {
var self = this;
selfpanyName = function()
return model.CompanyName;
};
}
Then I want to use this function like <span data-bind="text: panyName" />
However, the JavaScript function is not evaluated and returned as text.
I go through the examples of Knockout on the web, but all of them are using puted observables.
Is there a way to achive this ? Thanks.
I am having issues with defining a function inside my viewmodel.
I fetch json data via jquery getJSON and map this data to my viewmodel.
$.getJSON('/Company/GetCompanies', function(data) {
var viewModel = new CompanyViewModel()
viewModel.model = ko.mapping.fromJS(data)
ko.applyBindings(viewModel)
});
Below is my viewmodel. As you can see what I want to do is, returning one of the properties of viewmodel via function called panyName
var CompanyViewModel = function() {
var self = this;
self.panyName = function()
return model.CompanyName;
};
}
Then I want to use this function like <span data-bind="text: panyName" />
However, the JavaScript function is not evaluated and returned as text.
I go through the examples of Knockout on the web, but all of them are using puted observables.
Is there a way to achive this ? Thanks.
Share Improve this question asked Jul 5, 2012 at 11:34 emre nevayeshiraziemre nevayeshirazi 19.3k12 gold badges67 silver badges82 bronze badges 4- 1 Why don't you want to use a puted observable? – aaberg Commented Jul 5, 2012 at 12:18
- I can use of course but I don't need panyName to be observed. I am wondering what is the issue with my code – emre nevayeshirazi Commented Jul 5, 2012 at 12:36
-
It shouldn't be
return self.CompanyName;
? – Ingro Commented Jul 5, 2012 at 14:09 - return statement might be wrong, but return 'text'; is not even working. – emre nevayeshirazi Commented Jul 5, 2012 at 14:14
2 Answers
Reset to default 4Try this:
var CompanyViewModel = function(data) {
ko.mapping.fromJS(data, {}, this);
};
CompanyViewModel.prototype.fileTaxes = function() {
console.log("Company is filing taxes.");
};
$.getJSON('/Company/GetCompanies', function(data) {
// data would look something like this:
// data: { panyName : "MicroStrategy",
// founderName : "etc" }
var viewModel = new CompanyViewModel(data);
ko.applyBindings(viewModel)
});
I've made some test, and this works for me:
return self.model()[0].CompanyName;
And call it with: data-bind="text: panyName()"
EDIT:
var CompanyViewModel = function() {
var self = this;
self.panyName = function(){
return self.model()[0].CompanyName;
};
}
$.getJSON('/Company/GetCompanies', function(data) {
var viewModel = new CompanyViewModel();
viewModel.model = ko.mapping.fromJS(data);
ko.applyBindings(viewModel);
});
This works assuming that your JSON data is returned in a format like:
[{"CompanyName":"Stack","SomeOtherField":"SomeOtherValue"},...];
and that you have only one pany inside it.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745124286a4612598.html
评论列表(0条)