最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to define function inside knockout viewmodel that is filled with Json data - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 4

Try 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.

发布评论

评论列表(0)

  1. 暂无评论