Sco*_*ott 11 javascript updating ko.observablearray knockout.js
我有以下代码:
// First we define our gift class, which has 2 properties:
// a Title and a Price.
// We use knockout js validation to ensure that the values input are suitable/
function Gift(item)
{
var self = this;
self.Title = ko.observable(item.Title);
// attach some validation to the Title property courtesy of knockout js validation
self.Title.extend({
required: true,
minLength: 3,
pattern: {
message: 'At least ',
params: '^[a-zA-Z]+\s?[a-zA-Z]*'
}
});
self.Price = ko.observable(item.Price);
self.Price.extend({required:true,number:true,min:0.1,max:1000});
};
var viewModelForTemplated =
{
gifts: ko.observableArray(), // gifts will be an array of Gift classes
addGift: function ()
{
this.gifts.push(new Gift({ Title: "", Price: "" }));
},
removeGift: function (gift)
{
this.gifts.remove(gift);
},
totalCost: ko.computed(function () {
if (typeof gifts == 'undefined')
return 0;
var total = 0;
for (var i = 0; i < gifts().length; i++)
{
total += parseFloat(gifts()[i].Price());
};
return total;
})
}
$(document).ready(function ()
{
// load in the data from our MVC controller
$.getJSON("gift/getdata", function (allGifts)
{
var mappedgifts = $.map(allGifts, function (gift)
{
return new Gift(gift);
});
viewModelForTemplated.gifts(mappedgifts);
});
ko.applyBindings(viewModelForTemplated, $('#templated')[0]);
}
Run Code Online (Sandbox Code Playgroud)
然后(在脚本上方)
<div id="templated">
<table >
<tbody data-bind="template: { name: 'giftRowTemplate', foreach: gifts }"></tbody>
</table>
<script type="text/html" id="giftRowTemplate">
<tr>
<td>Gift name: <input data-bind="value: Title"/></td>
<td>Price: \$ <input data-bind="value: Price"/></td>
<td><a href="#" data-bind="click: function() { viewModelForTemplated.removeGift($data) }">Delete</a></td>
</tr>
</script>
<p>Total Cost <span data-bind="text: totalCost"></span> </p>
<button data-bind="click: addGift">Add Gift</button>
<button data-bind="click: save">Save</button>
</div>
Run Code Online (Sandbox Code Playgroud)
totalCost方法只运行一次,当礼品数组为空时,我可以将项目推送或删除到observableArray()没问题,但没有任何问题.
如何获得指向totalCost更新的范围?我打赌这很简单:)
谢谢你的帮助.
Ada*_*kis 12
你需要解开你的观察:
totalCost: ko.computed(function () {
//also, you forgot typeof below
if (typeof gifts == 'undefined')
return 0;
var total = 0; //here \/
for (var i=0; i < gifts().length; i++)
{ //and here \/
total += parseFloat(gifts()[i].Price());
};
return total;
})
Run Code Online (Sandbox Code Playgroud)
它没有更新的原因是因为
gifts.length
Run Code Online (Sandbox Code Playgroud)
总是在评估0,从不进入循环.即使它做了,
gifts[i].Price()
Run Code Online (Sandbox Code Playgroud)
不会出于同样的原因; 你需要解开观察.
请注意,当您不解包时,长度求值为零的原因是因为您获得了实际可观察数组函数的长度.Knockout中的所有可观察对象都是作为常规函数实现的; 当你不解开它时,你正在击中实际的函数本身,而不是底层的数组.
编辑,
此外,您需要引用礼物this.gifts,因为它是一个对象属性.这就是为什么这不起作用; 礼物总是不确定的.
也就是说,你还需要做更多的工作来让ko计算器从对象文字中工作.在这里阅读更多信息:
http://dpruna.blogspot.com/2013/09/how-to-use-kocomputed-in-javascript.html
以下是我制作视图模型的方法:
function Vm{
this.gifts = ko.observableArray(); // gifts will be an array of Gift classes
this.addGift = function () {
this.gifts.push(new Gift({ Title: "", Price: "" }));
};
this.removeGift = function (gift)
{
this.gifts.remove(gift);
};
this.totalCost = ko.computed(function () {
var total = 0;
for (var i = 0; i < this.gifts().length; i++)
{
total += parseFloat(this.gifts()[i].Price());
};
return total;
}, this);
}
var viewModelForTemplated = new Vm();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15923 次 |
| 最近记录: |