3
我試圖通過AJAX接收數據並將其顯示在頁面上。 ajax請求成功,但淘汰賽不會更新視圖。可能是什麼問題?Knockout.js,CoffeeScript和jQuery ajax不更新文本字段
源代碼:視圖模型
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>
您還需要更新'companyTariff'和'companyBalance'計算的觀察值以讀取公司,然後閱讀相應的屬性。但是我不明白coffeescript能夠寫出來(它看起來足夠接近javascript,我明白它在做什麼,不足以讓我自己寫)。 –