我正在尝试通过 AJAX 接收数据并将其显示在页面上。ajax请求成功,但是knockout没有更新视图。可能是什么问题?
源代码:查看模型
class AppViewModel
constructor: ->
@company = ko.observable {name:'n/a', tariff:'n/a', contract:'n/a', balance:'0'}
@getBriefInfo()
@companyTariff = ko.computed => "Tariff: #{@company.tariff}"
@companyBalance = ko.computed => "Total: #{@company.balance}"
getBriefInfo: ->
$.ajax
type: 'POST'
url: '/index.php/site/getCompanyShortInfo'
data: {}
dataType: 'json'
contentType: 'json'
success: (res) =>
console.log @company(), res.name, res.tariff
@company res
console.log @company(), res.name, res.tariff
$ ->
ko.applyBindings(new AppViewModel(),document.getElementById("company-info"))
源代码:查看
<ul id="company-info" class="unstyled company-info">
<li data-bind="text: company.contract"></li>
<li data-bind="text: company.name"></li>
<li data-bind="text: companyBalance"></li>
<li data-bind="text: companyTariff"></li>
</ul>