javascript - Assigning KnockoutJS object from $.get -
i have next code , data
{ "job": { "jobname": null, "jobnumber": 13, "jobsize": 0, "status": "finished", "datecreated": "/date(1312551946280)/" }, "values": [ { "id": "8401", "status": "good", "jobnumber": 13 }, { "id": "8402", "status": "bad", "jobnumber": 13 } ] } function jobviewmodel() { var self = this; self.jobs = ko.observablearray(); self.selectedjob = ko.observable(); self.history = ko.observablearray(); var app = sammy(function () { this.get('#/:jobnumber', function (context) { $.get('/home/getjobinfo/' + context.params.jobnumber, {}, self.selectedjob); }); }); jquery(function () { app.run(); }); }
i know $.get working because can see json info coming back, , have used function in 3rd parameter on $.get see data.
when replace function self.selectedjob property why not beingness populated?
thanks
update: here html
<!doctype html> <html> <head> <link href="~/content/main.css" rel="stylesheet" /> <title>sentinel web data</title> </head> <body> <div id="sidebar"> <ul data-bind="foreach: jobs"> <li><a data-bind="text: $data.jobname, attr: { href: '#/' + $data.jobnumber}"></a></li> </ul> </div> <div id="job"> <span data-bind="text: jobnumber"></span> </div> <div class="clear"></div> <div id="history" data-bind="with: history"></div> <script src="~/scripts/knockout-2.2.1.js"></script> <script src="~/scripts/jquery-1.4.4.min.js"></script> <script src="~/scripts/sammy-0.7.4.min.js"></script> <script src="~/scripts/jobviewmodel.js"></script> <script> ko.applybindings(new jobviewmodel()); </script> </body> </html>
when looking @ in console , having watch on self.selectedjob.job "undefined"
regarding console, looking @ incorrectly. see selected job name, need use:
var vm = new jobviewmodel(); ko.applybindings(vm);
then in console:
vm.selectedjob().job
as binding html, you're not binding selected job:
<div id="job" data-bind="with: selectedjob"> <span data-bind="text: jobnumber"></span> </div>
javascript knockout.js
No comments:
Post a Comment